【发布时间】:2020-04-05 03:43:59
【问题描述】:
当我尝试执行错误、回调功能时,以下代码返回未定义。我不明白为什么 json 没有返回,因为控制台日志输出了完整的查询。
这是将 json 生成到日志中的调用函数。
exports.getThem = (req) => {
var addy = req.body.email;
Picks.findOne({ 'email' : addy }, (err, theResults) => {
if (err) {
return ({ 'msg': err });
}
console.log("made the query " + JSON.stringify(theResults));
return theResults;
});
};
在上面,结果打印“进行了查询”和一个完整的 JSON 字符串。
调用代码将执行以下 JSON 并将其返回给 Postman,但不包含 theResults JSON。
console.log "log this" 永远不会执行,第二个 log 打印 "after the log" undefined。
{
"token" : someCrazyLookingToken
}
exports.loginUser = (req, res) => {
var theResults;
theResults = Picks.getPicks( req , ( err , thePicks) => {
console.log("log this" + JSON.stringify(thePicks));
if (!err){
console.log ("what happened" + err)
}
});
console.log("after the call " + JSON.stringify(theResults));
return res.status(200).json({ picks: thePicks, token: createToken(user) });
}
为什么?回调中是否存在我没有等待的时间问题?
【问题讨论】:
标签: javascript node.js express