Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js中的回调函数即将推出_Node.js_Mongodb_Express - Fatal编程技术网

node.js中的回调函数即将推出

node.js中的回调函数即将推出,node.js,mongodb,express,Node.js,Mongodb,Express,我有一个javascript数组,如下所示: [[1,2,3],[4,5,6]] 1、2、3、4、5和6是我的追随者id 完成查找操作后。我想将输出发送到浏览器 有人能告诉我如何才能出来或将结果发送到浏览器吗 for(var i = 0; i<idName.length; i++ ) { tempJson = {user_id: followerId, feed_content_text: finalFeedText }; notifications.push(tempJson)

我有一个javascript数组,如下所示:
[[1,2,3],[4,5,6]]

1、2、3、4、5和6是我的追随者id 完成查找操作后。我想将输出发送到浏览器

有人能告诉我如何才能出来或将结果发送到浏览器吗

 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
        });
  });
 }

for(var i=0;i您尝试过类似的方法吗

res.status(200).json({
        success: true,
         message: finalMsgs
        });

您是否尝试过以下方法:

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
        });
   }
  });
 }
var result=[];//初始化结果数组

对于(var i=0;i而言,在我的情况下,这很好:

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
                });
        }
  });
 }
var userIndex=0;

对于(var i=0;我想知道“条件”这应该在findOne内部使用。当所有迭代都执行时,findOne将执行。Dop知道如何发送输出。他想知道,如何在完成后发送响应。我感谢你的回答。但这在我的情况下不起作用。我尝试了一个新的解决方案,在我的情况下效果很好。让我发布答案for循环将完成在所有
FeedModel.findOne()函数完成之前很久。您需要另一个仅在回调中递增的变量。或者,此设计模式(在回调中递增以检测完成的计数器)由
async.parallel()封装
async.js中的函数然后我建议您使用
async.parallel()
.Hmm,它是由于其他原因而出现的。next()两次。当头发送两次时,头已发送错误出现。在上述解决方案中也会发生同样的情况。我的条件是if(userIndex