Reputation: 11
I have a file with the following function. This function returns JSON. I want to call this function from another file.
exports.me = function(req, res) {
var userId = req.user._id;
User.findOne({
_id: userId
}, function(err, user) {
if (err) return next(err);
if (!user) return res.json(401);
res.json(user);
});
};
I'm doing following but i get res.json is not a function, I dont know how to receive a json to a variable. Also looks like an async problem. Please help.
var user = require('user');
var loggedIn = user.me(req, function(user){
return user;
});
Upvotes: 0
Views: 3961
Reputation: 2456
The me()
function appear to be an express request event handler. Therefore the res variable is expected to be a response object. In your code you are passing a function as second argument and thats the reason of the error.
Additionally the me()
function doesn't have a return value and therefore the value of the loggedIn
variable will be undefined.
Upvotes: 1