nodeJS创建动态数组并用作响应

时间:2015-12-06 03:57:55

标签: javascript arrays node.js mongodb express

您好我正在创建一个空数组,然后使用forEach循环使用来自mongo查询的数据填充它。

我现在已经尝试了4天了,我似乎没有做任何工作我知道我很接近但是作为javascript和MEAN堆栈的新手我只是无法弄明白。

我附上了代码,并附有我想要做的所有事情的评论。

请任何帮助都很棒......

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

                });                  
            }   
    });  
};

2 个答案:

答案 0 :(得分:1)

您需要将您的callback();这是在您的async.forEach()回调结束User.findOne({_id: user}, ...)回调(调用后立即obj.push()),因为这是里面的时候你实际上完成了item。使用当前代码,您可以在mongo查询有机会完成之前立即告知async模块item

答案 1 :(得分:1)

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

                });                  
            }   
    });  
};