The code I wrote below is part of the controller user.js
exports.login = function(req, res) {
var db = require('../db').tables;
//db.user.find(function(err, user) { console.log(user) });
switch (req.method) {
default:
case 'GET':
break;
case 'POST':
db.user.find({username: req.body.username}, function(err, user) {
});
console.log(user);
break;
}
res.render('user/login', {title: 'Login'});
};
The problem is the function db.user.find and I do not have available the variable res within the callback.
I have already tried several times to re-read the documentation mongoose to use a syntax more comfortable but nothing.
How do you advise me to change the code?
Sorry for bad English but I used Google. :(
Because the anonymous callback function for find
is defined within the context of the login
function, your callback already has access to res
via closure.
...
db.user.find({username: req.body.username}, function(err, user) {
// code here can reference res from the enclosing scope
res.json(user);
});
...