i am using for loop to retrieve the data from mongodb and display it on the browser but the problem it it iterates only for the first time and stops. So i get only the first entry as the output. Whereas it gives the correct output in the console log. Please help?
var movieSchema1 = new mongoose.Schema({
name:String,
address:String,
location:String
});
var user=mongoose.model('movie', movieSchema1, 'movie');
for (var i = 0; i < user.length; i++) {
res.json("iteration " + i);
res.json('name:' + user[i].name + user[i].address);
}
res.json()
sends an object and then ENDs the response. You cannot ever call it more than once on the same response object and expect it to work. Build your object in the loop and thenres.json()
it at the end.