想要在 node.js 中同步发送请求
Posted
技术标签:
【中文标题】想要在 node.js 中同步发送请求【英文标题】:Want to send request in sync in node.js 【发布时间】:2015-02-10 03:09:18 【问题描述】:我有一个场景,我想从集合中获取数据并插入到数组中,然后返回该数组。
router.route('/user/:_id/host/:_id/joineeList').get(function(req, res)
var finalList = [];
Host.findOne(
_id : req.params._id,
, function(err, host)
if (err)
res.send(err);
else
var count = host.joinees.length;
for (var i = 0; i < count; i++)
console.log(host.joinees[i].userid);
var ID = host.joinees[i].userid;
var CODE = host.joinees[i].status_code;
User.findOne(
_id : host.joinees[i].userid
, function(err1, user)
if (err1)
console.log(err1);
else
finalList.push(
"userId" : ID,
"name" : user.name,
"profilepic" : user.profilepic,
"status" : CODE
);
console.log(finalList);
// finalList[i].add = ("userId",ID);
// finalList[i].add = ("name",user.name);
// finalList[i].add = ("profilepic",user.profilepic);
// finalList[i].add = ("status",CODE);
);
);
);
现在,发生的事情是我的数组返回 null,但数据也插入到 finalList 数组中。这是控制台输出:
[]
888888888888888888888888888888888888888888
[ userId: '5485ae1159a751697e000003',
name: 'aaaa',
profilepic: 'https://graph.facebook.com/986962491123456/picture?type=large',
status: 0 ]
------------------------------------------
[ userId: '5485ae1159a751697e000003',
name: 'aaaa',
profilepic: 'https://graph.facebook.com/123456781319929/picture?type=large',
status: 0 ,
userId: '5485ae1159a751697g7654003',
name: 'ssss',
profilepic: 'link',
status: 0 ]
------------------------------------------
【问题讨论】:
你不会在节点中同步做事。为什么要同步执行并阻止所有内容?使用 Promise 或回调来实现,以便在您启动的所有异步任务完成时做出响应。 【参考方案1】:你可以使用这个库 - https://github.com/caolan/async#eachSeries。方法是eachSeries
您的代码如下所示:
var async = require('async');
router.route('/user/:_id/host/:_id/joineeList').get(function (req, res)
var finalList = [];
Host.findOne(
_id: req.params._id
, function (err, host)
if (err)
res.send(err);
else
var count = host.joinees.length;
var limits = [];
for (var i = 0; i < count; i++)
limits.push(i);
async.eachSeries(limits, function (i, callback)
console.log(host.joinees[i].userid);
var ID = host.joinees[i].userid;
var CODE = host.joinees[i].status_code;
User.findOne(
_id: host.joinees[i].userid
, function (err1, user)
if (err1)
console.log(err1);
else
finalList.push(
"userId": ID,
"name": user.name,
"profilepic": user.profilepic,
"status": CODE
);
callback();
);
, function()
console.log(finalList);
);
);
);
如果有帮助请告诉我
【讨论】:
感谢您的帮助!当我尝试使用您的代码时,它显示错误。错误是“ReferenceError: startAt is not defined” @KanishkaSen 哎呀。它应该是i
而不是 startAt
。我已经更新了答案 - 立即尝试
我还有一个疑问。你能看一下吗。这是链接:- ***.com/questions/27433032/…【参考方案2】:
感谢@Vitaliy Zurian。我通过在 for 循环中使用计数器来使用以下代码
router.route('/user/:_id/host/:_id/joineeList').get(function (req, res)
var finalList = [];
Host.findOne(
_id: req.params._id
, function (err, host)
if (err)
res.send(err);
else
var count = host.joinees.length;
var limits = [];
for (var i = 0; i < count; i++)
limits.push(i);
async.eachSeries(limits, function (i, callback)
console.log(host.joinees[i].userid);
var ID = host.joinees[i].userid;
var CODE = host.joinees[i].status_code;
User.findOne(
_id: host.joinees[i].userid
, function (err1, user)
if (err1)
console.log(err1);
else
finalList.push(
"userId": ID,
"name": user.name,
"profilepic": user.profilepic,
"status": CODE
);
callback();
);
, function()
console.log(finalList);
);
);
);
【讨论】:
【参考方案3】:您可能需要异步库的并行方法(或者可能是映射)检查一下https://github.com/caolan/async#parallel
【讨论】:
以上是关于想要在 node.js 中同步发送请求的主要内容,如果未能解决你的问题,请参考以下文章