Sunday, 23 April 2017

Loop through array of id's and return documents which match with Mongoose

I currently have an array which looks as follows (all id's are the same for testing purposes): enter image description here

What I need to do is loop through this array and return the user associated with these Id's but cant seem to get it working.

 $.ajax({
     url: "/findUsers",
     type: "POST",
     dataType: "json",
     data: {
        array:array
     },
     success: function (data) {

     }
   })

post request:

app.post('/findUsers', function(req, res){
    var array = req.body;
    console.log(array);
    for(var i = 0; i < array.length; i++) {

      User.find({'_id': array[i]}).exec(function(err, docs){
                 console.log(docs);
            });
         }
        });

I can log the array server side but nothing is returned to me?



via user

No comments:

Post a Comment