简体   繁体   中英

nodeJS create dynamic array and use as response

Hi i am creating an empty array then populating it with data from a mongo query using a forEach Loop.

i have been attempting this now for 4 days and nothing i seem to do is working i know im close but being a newbie to javascript and MEAN stack i just cant figure it out.

i have attached the code with comments on everything i am trying to do.

please any help would be awesome..

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

                });                  
            }   
    });  
};

You need to move your callback(); that is at the end of your async.forEach() callback to inside the User.findOne({_id: user}, ...) callback (right after you call obj.push() ) because that is when you are actually done with item . With your current code you are telling the async module immediately that you are done with item , before your mongo query has a chance to complete.

mscdex

Was spot on his answer solved my issue for future help to others here is the code

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

                });                  
            }   
    });  
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM