在以下功能中,我的理解是,如果满足条件,则代码应在回调后停止执行如果"!clean.postid == true"。当我在测试过程中遇到第二条条件时,将带有错误消息调用回调,但是仍执行以下块(data.updatepost(((,导致再次调用回调。我在这里做错了什么?感谢您的帮助!
var data = require('./data')
var validate_post = require('./validate_post')
module.exports = function(post, callback) {
validate_post(post, function(err, clean) {
if(err) {
callback({status: "error", message: err})
return
}
if(!clean.postId) {
console.log('first callback is called at this point')
callback({status: "error", message: "Something is off here"})
return
}
console.log('code still executes for some reason')
data.updatePost(clean.postId, clean.post, function(err, res) {
if (err) {
callback({status: "error", message: err})
return
}
console.log('callback is called a second time at this point')
callback(null, {status: "success", message: res})
})
})
}
事实证明,我忘了将返回语句放在validate_post()
函数中,导致validate_post()
的回调两次。修复validate_post()
功能后,我的问题现在可以解决。谢谢!