res.locals returned undefined
express, node.js, templating
Solution
You have your request & response objects backwards. Obviously if you know the difference in your code, naming doesn't matter, but best to keep things named correctly.
app.use( function (request, response, next) {
// stuff
});
I can't recall off the top of my head, but I believe you want:
request.app.locals;
using my example above. Again, not 100% sure. You can always console out the request object to check.
Problem
I'm trying to set some response-specific variables, and I'm getting `undefined` for `res.locals` when I log it from within my middleware, but it returns the function just fine if I log it from within a route function. ``` // all environments app.set('port', process.env.PORT || 3000); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodOverride()); app.use(function (res, req, next) { console.log("res.locals from app.use middleware: ", res.locals); // res.locals.boom = 'nice'; // res.locals.zoom = 'yeah!'; next(); }); app.use(app.router); app.use(require('stylus').middleware(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public'))); ``` Any ideas?