找到你要的答案

Q:Coming out of call back function in node.js

Q:走出回调函数在Node.js

I have a javascript array like this: [[1,2,3], [4,5,6]]

1,2,3 and 4,5, and 6 are my follower id's After completing the find operation. I want to send output to browser.

Can anybody let me know how can I come out or send the result to browser?

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

I have a javascript array like this: [[1,2,3], [4,5,6]]

1,2,3 and 4,5, and 6 are my follower id's After completing the find operation. I want to send output to browser.

谁能告诉我怎样才能出来或者将结果发送到浏览器?

 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
        });
  });
 }
answer1: 回答1:

Have you tried this following:

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 = [];// 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
        });
   }
  });
 }
answer2: 回答2:

Have you tried something like that?

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

你试过那样的东西吗?

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

This is working fine in my case:

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;
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
                });
        }
  });
 }
node.js  mongodb  express