0

客户端发送一组 JSON 消息以存储在 Nodejs 服务器上;但是客户端需要对每条消息进行某种确认(通过唯一的 id),它已正确存储在服务器上,因此不需要再次发送。

在服务器上,我想解析 JSON 数组,然后循环遍历它,将每条消息存储在 db 中,将此消息的响应存储在名为响应的 JSON 数组中,最后将此响应数组发送到客户端。但是由于 db 操作是异步的,所有其他代码都在从 db 存储方法返回任何结果之前执行。我的问题是如何不断更新响应数组,直到所有数据库操作完成?

var message = require('./models/message');
var async = require('async');

var VALID_MESSAGE = 200;
var INVALID_MESSAGE = 400;
var SERVER_ERROR = 500;

function processMessage(passedMessage, callback) { 
var msg = null;
var err = null;  
var responses = [];

isValidMessage(passedMessage, function(err, result) {
if(err) {
  callback( createResponse(INVALID_MESSAGE, 0) );
}else{ 
 var keys = Object.keys(result);
for(var i=0, len = keys.length; i<len; i++) {
async.waterfall([     
  //store valid json message(s)
  function storeMessage(callback) {   
    (function(oneMessage) {
      message.processMessage(result[i], function(res) {
        callback(res, result[i].mid, callback);  
      });
    })(result[i]);
    console.log('callback returns from storeMessage()');
  },

  //create a json response to send back to client
  function createResponse(responseCode, mid, callback) {
    var status = "";
    var msg = "";

    switch(responseCode) {
    case VALID_MESSAGE: { 
      status = "pass";
      msg = "Message stored successfuly.";
      break;
    }
    case INVALID_MESSAGE: {
      status = "fail";
      msg = "Message invalid, please send again with correct data.";
      break;
    }
    case SERVER_ERROR: {
      status = "fail";
      msg = "Internal Server Error! please contact the administrator.";
      break;
    }
    default: {

      responseCode = SERVER_ERROR;
      status = "fail";
      msg = "Internal Server Error! please contact the administrator.";
      break;
    }
  }
  var response = { "mid": mid, "status": status, "message": msg, "code": responseCode};   
  callback(null, response );   
  }                     
],

function(err, result) {
  console.log('final callback in series: ', result);
  responses.push(result);          
});
}//loop ends
}//else ends
console.log('now we can send response back to app as: ', responses);  
});//isValid finishes
}
4

2 回答 2

1

为了扩展 lanzz 所说的,这是一个非常常见的解决方案(同时启动多个“任务”,然后使用通用回调来确定它们何时全部完成)。这是我的 userStats 函数中的函数的快速粘贴,它获取活跃用户的数量(DAU、WAU 和 HAU):

exports.userStats = function(app, callback)
{
    var res = {'actives': {}},
       day = 1000 * 60 * 60 * 24,
       req_model = Request.alloc(app).model,
       actives = {'DAU': day, 'MAU': day*31, 'WAU': day*7},
       countActives = function(name, time) {
           var date = new Date(new Date().getTime() - time);
           req_model.distinct('username',{'date': {$gte: date}}, function(e,c){ 
               res.actives[name] = parseInt(c ? c.length : 0, 10);
               if(Object.keys(actives).length <= Object.keys(res.actives).length)
                   callback(null, res);
           });
       };


    var keys = Object.keys(actives);
    for(var k in keys)
    {
        countActives(keys[k], actives[keys[k]]);
    }
};
于 2012-10-18T17:50:49.720 回答
1

responses仅当其中的项目数等于对象中的键数时才发送您的数组result(即您已经收集了所有这些的响应)。推送数组中的每个响应后,您可以检查是否可以发送。

于 2012-10-18T15:29:36.280 回答