lundi 12 février 2018

ejs ignoring if statement if a css class is added to the element

I'm having a strange problem with ejs template engine in a nodejs, express app, that it's ignoring the if statement and showing the html inside. Here's the ejs code below: This work and if statement is effective:

<% if (typeof success !== "undefined") { %>
    //this works fine without the css class
     <h4><%= success %></h4>

<% } %>

This however show up regardless: 

<% if (typeof success !== "undefined") { %>
    //this div is showing up regardless when adding the bootstrap classes
    <div class="alert alert-success"><%= success %></div>
<% } %>

I'll include the app.js code as well below if needed :

const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const session      = require('express-session');
const bodyParser = require('body-parser');
const port       = 5000;
const mongoose = require('mongoose');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const flash = require('connect-flash');
const engine = require('ejs-mate');

const app = express();

app.use(logger('dev'));
app.use(bodyParser.json() );
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// view engine setup
app.engine('ejs', engine);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));

app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true
}));
app.use(flash());
app.use(function(req, res, next) {
  res.locals.currentUser = req.user;
    res.locals.error = req.flash("error");
    res.locals.success = req.flash("success");
    next();
});
//routes
const indexes = require('./routes/indexes');
const diaries = require('./routes/diaries');
const users = require('./routes/users');



// Configure passport middleware
app.use(passport.initialize());
app.use(passport.session());

// Configure passport-local to use account model for authentication
const db = require('./models');
passport.use(new LocalStrategy(db.User.authenticate()));

passport.serializeUser(db.User.serializeUser());
passport.deserializeUser(db.User.deserializeUser());
app.use('/users', users);
app.use('/', indexes);
app.use('/diaries', diaries);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

app.listen(port, ()=>{
  console.log(`App started on port ${port}`);
});
module.exports = app;

I've been going around in circles for awhile, so any help would be highly appreciated.

Aucun commentaire:

Enregistrer un commentaire