想要在节点.js中同步发送请求
Want to send request in sync in node.js
我有一个场景,我想从集合中获取数据并插入到数组中,然后返回该数组。
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 } ]
------------------------------------------
>您可以使用此库 - 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);
});
}
});
});
让我知道它是否有帮助
您可能需要异步库的并行方法(或者可能是映射)检查出来 https://github.com/caolan/async#parallel
感谢@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);
});
}
});
});
相关文章:
- 根据最新响应在 Node 中发送同步请求
- 同步请求谷歌地图地理编码
- 火狐插件 SDK 同步请求
- 以发送的相同顺序显示 AJAX 响应,而不使用同步请求
- jquery 1.4,同步请求
- 如何为JQuery$.ajax同步请求设置超时并执行某些操作
- 节点4.2.0 phantom stdout:NETWORK_ERR:XMLHttpRequest异常101:同步请求中
- 正在包装主干同步请求
- RequireJS:同步请求文本内容
- mongodb同步请求
- Marketo-Munchkin同步请求损害了JS的性能
- 节点.js中的同步请求
- 循环中与 ajax 的同步请求
- 用于缓存的Node.js同步请求
- js 1.10, jquery 2.1和同步请求
- AJAX同步请求和操作,同时等待
- 我如何在主干运行一个基本的GET /同步请求
- 为什么异步xmlhttprequest优于同步请求?
- 使用JavaScript同步请求图像
- HTTP同步请求阻止我的GIF在Chrome和IE中移动