我无法弄清楚如何将对象结果传递回函数。我正在尝试回调,但它无法正常工作。我究竟做错了什么?有谁知道如何将变量从子函数传递给父函数? 我似乎也无法做到这一点。
findOrCreate(fb_response, function(error, result) {
if (error) {
res.serverError(error);
}
return res.send(result);
});
// If no user create new user
function findOrCreate(fb_response, callback) {
Users.findOne({
email: fb_response.email
}).exec(function(error, result) {
if (error) {
return callback(error, null);
}
// if user does not exist
else if (!result) {
//create a new user
createUser(fb_response, function(error, result) {
if (error) {
return callback(error, null);
}
// re-start the find or create function
return findOrCreate(fb_response, null);
}); // end of create new user function.
} // end of - else if (!record)
else if (result) {
return callback(null, result) // this is the part that I am getting wrong
}
});
}

答案 0 :(得分:1)
问题主要在于您的
return findOrCreate(fb_response, null);
当您将回调函数作为null传递给它时,并且在执行它时从不检查它是否为null。尝试将其更改为
return findOrCreate(fb_response, callback);