Javascript 节点创建动态数组并用作响应
嗨,我正在创建一个空数组,然后使用forEach循环从mongo查询中填充数据 我已经尝试了4天了,我似乎什么都没做,我知道我很接近,但作为javascript和MEAN stack的新手,我就是想不出来 我已经附上了我试图做的每件事的注释代码 求求你,任何帮助都会很棒Javascript 节点创建动态数组并用作响应,javascript,arrays,node.js,mongodb,express,Javascript,Arrays,Node.js,Mongodb,Express,嗨,我正在创建一个空数组,然后使用forEach循环从mongo查询中填充数据 我已经尝试了4天了,我似乎什么都没做,我知道我很接近,但作为javascript和MEAN stack的新手,我就是想不出来 我已经附上了我试图做的每件事的注释代码 求求你,任何帮助都会很棒 var mongoose = require('mongoose'), User = require('../../models/UserModel'), async = require('async');
var mongoose = require('mongoose'),
User = require('../../models/UserModel'),
async = require('async');
module.exports.getfollowing = function(req, res){
//grab the Users ID from the body
var thefollower = req.body.follower;
//create empty array that i want to populate with the followee's ID and Avatar url
var obj = [];
//query mongo for the user
User.findOne({ _id: thefollower }, function (err, user) {
if (err) {
console.log(err);
res.json(err);
} else {
//grab the following element of the users mongo schema -- should return all the followee's ID's -- tested works
var following = user.following;
//iritate throught all the followee's
async.forEach(following, function(item, callback) {
//current followee
var user = item;
//query mongo for the followee
User.findOne({_id: user}, function(err, followee, callback){
//get followee's ID and Avatar url
var id = followee._id;
var avatar = followee.avatar;
//add the followee's ID and Avatar url to the obj array
obj.push({
id: id,
avatar: avatar
});
});
//see if this worked - returns empty
console.log(obj);
callback();
}, function(err) {
//see if this worked - returns empty
console.log(obj);
//respond to the client - returns empty
res.json(obj);
});
}
});
};
您需要移动
回调()
位于async.forEach()
回调的末尾,在User.findOne({u id:User},…)
回调中(在调用obj.push()
之后),因为这是您实际完成项的时候。使用当前代码,您会在mongo查询有机会完成之前,立即告诉异步
模块您已完成项
。mscdex
在他的回答上当场解决了我的问题,以便将来帮助他人这里是代码
var mongoose = require('mongoose'),
User = require('../../models/UserModel'),
async = require('async');
module.exports.getfollowing = function(req, res){
//grab the Users ID from the body
var thefollower = req.body.follower;
//create empty array that i want to populate with the followee's ID and Avatar url
var obj = [];
//query mongo for the user
User.findOne({ _id: thefollower }, function (err, user) {
if (err) {
console.log(err);
res.json(err);
} else {
//grab the following element of the users mongo schema -- should return all the followee's ID's -- tested works
var following = user.following;
//iritate throught all the followee's
async.forEach(following, function(item, callback) {
//current followee
var user = item;
//query mongo for the followee
User.findOne({_id: user}, function(err, followee){
//get followee's ID and Avatar url
var id = followee._id;
var avatar = followee.avatar;
//add the followee's ID and Avatar url to the obj array
obj.push({
id: id,
avatar: avatar
});
//see if this worked - returns empty
console.log(obj);
callback();
});
}, function(err) {
//see if this worked - returns empty
console.log(obj);
//respond to the client - returns empty
res.json(obj);
});
}
});
};