我有一个像这样的javascript数组:
[[1,2,3], [4,5,6]]
1,2,3和4,5,以及6是我的追随者身份 完成查找操作后。我想将输出发送到浏览器。
有人可以告诉我怎么出来或将结果发送到浏览器?
for(var i = 0; i<idName.length; i++ ) {
tempJson = {user_id: followerId, feed_content_text: finalFeedText };
notifications.push(tempJson);
var conditions = {
user_id: followerId,
external_id: key
};
FeedModel.findOne(conditions, function (err, data) {
//What condition should i write to send the output to browser when, it is done for all 'idNames'
res.send({
success: true,
message: finalMsgs
});
});
}
答案 0 :(得分:2)
你有没有尝试过类似的东西?
res.status(200).json({
success: true,
message: finalMsgs
});
答案 1 :(得分:2)
您是否尝试过以下操作:
var result = [];// initialize result array
for(var i = 0; i<idName.length; i++ ) {
tempJson = {user_id: followerId, feed_content_text: finalFeedText };
notifications.push(tempJson);
var conditions = {
user_id: followerId,
external_id: key
};
FeedModel.findOne(conditions, function (err, data) {
result.push(data);//storing data in result
if((i+1) === idName.length) { // check if all is completed
res.send({ // sending response
success: true,
message: finalMsgs,
data: result
});
}
});
}
答案 2 :(得分:1)
在我的情况下这很好用:
var userIndex = 0;
for(var i = 0; i<idName.length; i++ ) {
userIndex++;
tempJson = {user_id: followerId, feed_content_text: finalFeedText
};
notifications.push(tempJson);
var conditions = {
user_id: followerId,
external_id: key
};
FeedModel.findOne(conditions, function (err, data) {
userIndex--; // It'll be 0 at the end
if ( userIndex == 0 ) {
return res.send({
success: true,
message: finalMsgs
});
}
});
}