[英]I got undefined from return function from model.find()
Here is my function that I want to return 这是我要返回的功能
var setUser = function(Email){
UserModel.findOne({email : Email}, function(err, data){
if(err) {
return Hapi.error.badRequest('There is no this user');
}
console.log(data);
return data;
});
};
The console.log(data); console.log(data); show the result that found.
显示找到的结果。 But When I use this function
但是当我使用这个功能
Example 例
console.log(setUser('123@mail.com'));
This log got undefined that returned from function. 该日志未定义,是从函数返回的。 But log in the function had data.
但是登录功能有数据。
.findOne
executes asynchronously and you can't return from the callback function ... that won't go anywhere. .findOne
异步执行,您无法从回调函数中返回……它不会随处可见。 Instead you would need to do something like: 相反,您将需要执行以下操作:
setUser('123@mail.com', console.log)
var setUser = function (Email, callback) {
// .. snip
callback(data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.