Note that there are some explanatory texts on larger screens.

plurals
  1. POExpressJS 3.0 How to pass res.locals to a jade view?
    primarykey
    data
    text
    <p>I want to display a flash message after a user fails to sign in but I just can't get the variables to show up in my Jade views. </p> <p>I have some pieces, I know I have to use this in my app.configure():</p> <pre><code> app.use (req, res, next) -&gt; res.locals.session = req.session </code></pre> <p>And I'll set what the flash message is after the user POSTS the wrong password:</p> <pre><code> exports.postSession = (req, res) -&gt; users = require '../DB/users' users.authenticate(req.body.login, req.body.password, (user) -&gt; if(user) req.session.user = user res.redirect(req.body.redirect || '/') else req.session.flash = 'Authentication Failure!' res.render('sessions/new', {title:'New', redirect: req.body.redirect }) ) </code></pre> <p>I don't know how to access <code>res.locals.session</code> in my Jade file. I doubt I am setting everything up right. This question is a lot like this one: <a href="https://stackoverflow.com/questions/11580796/migrating-express-js-2-to-3-specifically-app-dynamichelpers-to-app-locals-use">Migrating Express.js 2 to 3, specifically app.dynamicHelpers() to app.locals.use?</a> but I still can't get it to work. It would be much appreciated if someone could show me just a simple example of setting values in res.local and accessing them in a view.</p> <p>p.s. I do know about connect-flash but I need to understand how to make things available in views. </p> <p>This is my app:</p> <pre><code>app.configure(() -&gt; app.set('views', __dirname + '/views') app.set('view engine', 'jade') app.use(express.bodyParser()) app.engine('.jade', require('jade').__express) app.use(express.methodOverride()) app.use(express.cookieParser()) app.use(express.session({ store: new express.session.MemoryStore({reapInterval: 50000 * 10}), secret: 'chubby bunny' })) app.use(express.static(__dirname + '/public')) app.use((req, res, next) -&gt; res.locals.session = req.session next() ) app.use(app.router) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload