Javascript 需要循环回调的节点
我想更改从其中一个集合中获取的标记格式标记数据在一个数组中包含一些KCID,我使用该数组获取KC数据并插入标记单元以获取最终响应格式Javascript 需要循环回调的节点,javascript,node.js,callback,event-loop,asynccallback,Javascript,Node.js,Callback,Event Loop,Asynccallback,我想更改从其中一个集合中获取的标记格式标记数据在一个数组中包含一些KCID,我使用该数组获取KC数据并插入标记单元以获取最终响应格式 var newTags = Tags.map(function(TagUnit) { for (var i = 0; i < TagUnit.kcs.length; i++) { KCArray = []; KC.findById(TagUnit.kcs[i], function(error, data) {
var newTags = Tags.map(function(TagUnit) {
for (var i = 0; i < TagUnit.kcs.length; i++) {
KCArray = [];
KC.findById(TagUnit.kcs[i], function(error, data) {
KCMap = {};
KCMap['kc_id'] = data._id;
KCMap['kc_title'] = data.title;
KCArray.push(KCMap);
if (KCArray.length == TagUnit.kcs.length) {
TagUnit.kcs = KCArray;
}
});
}
return TagUnit;
});
response.send(JSON.stringify(newTags));
var newTags=Tags.map(函数(TagUnit){
对于(变量i=0;i
但我没有得到想要的结果。响应是以首字母形式而不是格式化形式给出标记数据。我猜这是由于事件循环。如果有人能帮我,我将不胜感激
**编辑:*我使用MongoDB作为数据库,mongoose作为ORM。您可以使用promises或module
另外,代码可能包含错误。我无法测试它。您可以使用承诺或模块
另外,代码可能包含错误。我不能测试它。我建议使用Promissions来管理异步操作,这现在是ES6的标准。您不需要说明正在使用的数据库(它可能已经有了基于承诺的接口)。如果没有,则手动提示
KC.findById()
:
然后,假设您可以并行执行所有这些查找操作,您可以使用Promise.all()
来跟踪这些操作的完成时间并为您排序
var allPromises = Tags.map(function(TagUnit) {
var promises = TagUnit.kcs.map(function(key) {
return findById(key).then(function(data) {
// make resolved value be this object
return {kc_id: data._id, kc_title: data.title};
});
});
// this returns a promise that resolves with an array of the kc_id and kc_title objects
return Promise.all(promises).then(function(results) {
return {
_id: TagUnit._id,
kcs: results
};
});
});
// now see when they are all done
Promise.all(allPromises).then(function(results) {
response.send(JSON.stringify(results));
}).catch(function(err) {
// send error response here
});
我建议使用承诺来管理您的异步操作,这现在是ES6中的标准。您不需要说明正在使用的数据库(它可能已经有了基于承诺的接口)。如果没有,则手动提示
KC.findById()
:
然后,假设您可以并行执行所有这些查找操作,您可以使用Promise.all()
来跟踪这些操作的完成时间并为您排序
var allPromises = Tags.map(function(TagUnit) {
var promises = TagUnit.kcs.map(function(key) {
return findById(key).then(function(data) {
// make resolved value be this object
return {kc_id: data._id, kc_title: data.title};
});
});
// this returns a promise that resolves with an array of the kc_id and kc_title objects
return Promise.all(promises).then(function(results) {
return {
_id: TagUnit._id,
kcs: results
};
});
});
// now see when they are all done
Promise.all(allPromises).then(function(results) {
response.send(JSON.stringify(results));
}).catch(function(err) {
// send error response here
});
你调查过了吗?您的问题是由于事件循环造成的,这是正确的<调用
response.send
时,很可能无法完全计算code>newTags。您查看了吗?您的问题是由于事件循环造成的,这是正确的newTags
在调用response.send
时很可能无法完全计算。我正在尝试测试解决方案。同时,仅供参考,我的数据库是mongoDB,ORM是mongoose。@Rusty-我有点难以说出您想要什么样的最终结果。此代码为您提供了一个对象数组,每个对象包含kc_id
和kc_title
,因为我认为您的原始代码正试图生成此数组。我希望响应采用以下格式<代码>{“tag_id”:“Nienemk2990HU38”,“kcs”:{“kc_id”:“nmdkwmswmwjm4944”,“kc_标题”:“ABC”}。您的解决方案似乎接近于我在进行最终格式化时遇到的困难。@Rusty-您确定吗?您的代码迭代了两个嵌套数组,看起来它生成了一个数组,其中每个kcs
属性本身就是一个数组:{“tag_id”:“nienemk29290hu38”,“kcs”:[{“kc_id”:“nmdkwmwjm4944”,“kc_标题”:“ABC”},{“kc_id”:“496jgwmwjm”,“kc_标题”:“DEF”}]}
。我可以更改我的数据库以生成此结果。这是您想要的吗?是的。您做对了。这正是我需要的。我正在尝试测试解决方案。同时,我的数据库是mongoDB,ORM是mongoose。@Rusty-我有点难以告诉您想要什么样的最终结果。此代码为您提供了一个数组对象数组每个对象都包含kc_id
和kc_title
,因为我认为这是您的原始代码试图生成的。我希望响应的格式如下。{“tag_id”:“nienemk2990hu38”,“kcs”:{“kc_id”:“nmdkwmwjm4944”,“kc_title”:“ABC”}
。您的解决方案似乎接近于我在进行最终格式化时遇到的困难。@Rusty-您确定吗?您的代码迭代了两个嵌套数组,看起来它生成了一个数组,其中每个kcs
属性本身就是一个数组:{“tag_id”:“nienemk29290hu38”,“kcs”:[{“kc_id”:“nmdkwmwjm4944”,”kc_title:“ABC”},{“kc_id:“496jgwmwjm”,“kc_title:“DEF”}]}
。我可以更改我的以生成此。这是您想要的吗?是的。您做对了。这正是我需要的。
var allPromises = Tags.map(function(TagUnit) {
var promises = TagUnit.kcs.map(function(key) {
return findById(key).then(function(data) {
// make resolved value be this object
return {kc_id: data._id, kc_title: data.title};
});
});
// this returns a promise that resolves with an array of the kc_id and kc_title objects
return Promise.all(promises).then(function(results) {
return {
_id: TagUnit._id,
kcs: results
};
});
});
// now see when they are all done
Promise.all(allPromises).then(function(results) {
response.send(JSON.stringify(results));
}).catch(function(err) {
// send error response here
});