我对Node.js相对较新,仍然围绕着回调和Node.js的异步特性。我遇到了一个我无法弄清楚的问题;我已经彻底搜索了溢出并阅读了支持文档,但仍然无法使其工作。
基本上,问题是我有一个对象数组,我想按顺序对每个对象应用一些异步操作。我查看过Async.js并尝试了各种回调结构,但是我的编译器一直在喊我。
我最新版本的代码是:
function processObjects(objects){
console.log(objects);
async.forEachOf(objects, function(object, key, callback){
var imageProfile;
var files = {};
function readFile(callback){
fm.mdfil("templates/no_object.jpg", function(result) { // Read metadata from no_object.jpg template
console.log("check1");
files['name'] = object + "_" + timestamp + ".jpg"; // file.name = object.id + timestamp
files['type'] = ".jpg";
files['size'] = result.size;
callback();
});
}
function updateFileProfileAgain(result, callback){
console.log("check2");
var obj = [];
obj.push(object);
f.insF(req.user._id, files.name, "", "", files.type, files.size, "", "", "", timestamp, obj, function(result){ // Add record of the image to the Files Collection
callback(null, result);
});
}
function updateImageRef(result, callback){
console.log("check3");
o.updSO(object, "imageRef", imageProfile.insertedId, function(result){
callback(null, result);
});
}
function updateObjectImage(result, callback){
console.log("check4");
o.updSO(object, "objectImg", files.name, function(result){
callback(null, result);
});
callback();
}
readFile(function(callback){
updateFileProfileAgain(function(result, callback){
updateObjectImage(function(result, callback){
updateImageRef(function(result, callback){
console.log(result);
});
});
});
});
readFile();
},
function(err, result){
console.log("done");
});
}
时间戳是一个全局变量。对象是具有唯一ID的数组。基本上我想解析所有这些唯一ID以给它们自己的图像(在readFile函数中),将该图像的记录添加到单独的集合(updateFileProfileAgain),将插入的记录的引用添加到对象' s记录(updateImageRef)并最终将图像记录的字符串添加到每个对象的objectImg字段(updateObjectImage)。
此代码的输出因未定义的回调(作为函数)和未定义的结果而异。在某些情况下,我确实在客户端上获得了结果,然后会显示一个对象的更新记录,但不会显示另一个对象(就好像文件变量没有更新或者它取了最后一个已知值),就像这样:
对于字段objectImg等也一样。
非常感谢任何帮助!
答案 0 :(得分:0)
您没有将每个函数的结果传递给下一个函数。
此外,回调可能是令人头痛的问题。我建议您使用Promise chaining
。
Objects.forEach(function (object) {
readFile(object)
.then(function(file) { return updateFileProfileAgain(file) })
.then(function(result) { return updateObjectImage(result) })
.then(function(image) { return updateImageRef(image) });
});
使用 ES6
Objects.forEach((object) => {
readFile(object)
.then(file => updateFileProfileAgain(file))
.then(result => updateObjectImage(result))
.then(image => updateImageRef(image));
});