nodejs将响应附加到列表中

我在一条路线上有多个res.send ,我怎样才能将它们全部追加到最后发送累积列表呢? 我更喜欢以下面的forms来做:

 { "writer": {success message}, "archive": {success message}, ... } 

和另一个像上面那样的列表错误。 这里是代码:

 router.post('/some/route', function (req, res) { if (req.isLoggedIn()) { return res.status(403).json({}); } MyModel.findById(req.user._id,function (err, data) { if(err || data.rights !== 'super'){ return res.status(403).json({}); } if(req.body.writer){ Books.update( { writer : req.body.id}, { $set : { writer : req.body.writer} }, function (err) { if(err){ res.status(500).send(err); } else{ res.status(200).send('updated successfully.'); } } ); }else{ Books.remove({writer: req.body.id}, function(err){ if (err){ return console.log(err)} }); } MetaInfo.findOneAndRemove({_id: req.body.id}, function (err, data) { console.log(err); }); Archive.findOne({_id: req.body.id},function (err, data) { smtpTransporter.sendMail({...}, function (error, response) { if (error) { console.log(error); } else { console.log("Mail sent"); } smtpTransporter.close(); }); data.remove(); if (err) { console.log(err); return res.status(200).json({ success: false, message: 'server error', err: err }); } res.status(200).json({ success: true }); }) }); }); 

我假设你的问题是对数据库的asynchronous调用。

所以最好采取一个你select的库(例如async ),并在callback中进行asynchronous处理,最后发送结果。

你的结果可能是这样的:

 async.parallel([ function(callback) { ... }, function(callback) { ... } ], function(err, results) { // send your result here });