Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点创建动态数组并用作响应_Javascript_Arrays_Node.js_Mongodb_Express - Fatal编程技术网

Javascript 节点创建动态数组并用作响应

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');

嗨,我正在创建一个空数组,然后使用forEach循环从mongo查询中填充数据

我已经尝试了4天了,我似乎什么都没做,我知道我很接近,但作为javascript和MEAN stack的新手,我就是想不出来

我已经附上了我试图做的每件事的注释代码

求求你,任何帮助都会很棒

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);  

                });                  
            }   
    });  
};