我正在尝试使用异步瀑布方法,但是当它到达其中一个功能时,它会挂起。我怀疑这是因为 save() 操作对于执行上下文来说太慢了,但这就是我开始使用异步瀑布的原因,所以我可以等待返回的值,直到它进入系列中的下一个函数(传递正确的数据,counted
在我下面的例子中)。
// 在我的用户控制器中:
async.waterfall([
function(callback) {
getSubmission(id, function(submission) {
if (submission) {
callback(null, submission);
}
});
},
function(submission, callback) {
var submissionId = submission._id;
getViews(submissionId, ip, function(count) {
if (count) {
callback(null, count, submissionId);
}
});
},
// Those top two functions work perfectly passing what
// I need to this one which is where I'm having trouble
function(views, submissionId, callback) {
// addView is called because it is actually
// inserting a row in the db, but never returns from the caller
addView(submissionId, ip, function(added) {
// this callback doesn't fire
if (added) {
callback(null, added);
}
});
},
function(added, callback) {
console.log(added);
}
]);
这就是addView()
(也在用户控制器中,前面的async.waterfall
代码也在其中):
var addView = function(submissionId, ip, callback) {
Submission.addView({
submissionId : submissionId,
ip: ip
}, function(err, counted) {
if (err) {
throw err;
}
if (counted) {
callback(counted);
}
});
};
这就是它在调用时调用的内容(在我的提交模型文件中)Submission.addView()
:
exports.addView = function(obj, fn) {
var ip = obj.ip,
submissionId = obj.submissionId,
submissionView = new SubmissionView(obj);
// it gets to this point
submissionView.save({
ip : ip,
submission_id : submissionId
}, function(err, counted) {
fn(err, counted);
});
};