0

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);
}
5
  • Where is user defined/initialized? Not shown in your code snippet above. Sep 10, 2017 at 19:41
  • res.json only wants to be called once. Build up your result first, maybe use an array for this.
    – Keith
    Sep 10, 2017 at 19:44
  • 1
    @Keith please help me with it?how to do it? Sep 10, 2017 at 19:45
  • @BrianDriscoll ya it is initialized Sep 10, 2017 at 19:46
  • 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 then res.json() it at the end.
    – jfriend00
    Sep 11, 2017 at 0:34

2 Answers 2

2

As others have said, build up an object for your response, then res.json() it. Because you're trying to send an array, just use .map() to transform each one:

//turn the array of users into the objects we want to send
const responseData = user.map( (movie, i) => {
   return {
      iteration: i,
      name: movie.name + ' ' + movie.address
   };
});

//this converts the array to a string for you
res.json( responseData );
6
  • Yes this works,but it gives the output in json format,i simply want to display the key and value without json format ?what changes need to be done Sep 10, 2017 at 20:04
  • nice answer! @DuncanThacker
    – C.Unbay
    Sep 10, 2017 at 20:12
  • You need to get each index and the value then write it to the response @RadhikaObhan check my update
    – C.Unbay
    Sep 10, 2017 at 20:13
  • 1
    If you don't want JSON, why are you using res.json()? Sep 10, 2017 at 20:26
  • @DuncanThacker what alternate can I use to display only key and value data? Sep 11, 2017 at 6:50
0

You can use find() method to receive all of the objects from a schema.

var response;  

user.find({}, function(err, users){
       if(err) throw err;
       if(users.length != 0){
          response = users.map((user, i) => {
             return {
               index: i,
               name: user.name + ' ' + user.address
             };             
       }
    })
    for(var i = 0; i < response.length; i++){
       res.write('Key: ' i + ' the value: ' response[i] + '\n');
    }
    res.end();

You should also use res.json() once only, as it will return Can't set headers after they are sent error.

Please see this link for info on res.json()

3
  • can we see your code? where does the res go to? @RadhikaObhan
    – C.Unbay
    Sep 10, 2017 at 19:59
  • @turmuka, you do the same as OP, trying to send response for each user. Use map to create an array of objects and after that send it.
    – alexmac
    Sep 10, 2017 at 20:00
  • true, I just saw the mistake there, gonna edit it in a sec @alexmac
    – C.Unbay
    Sep 10, 2017 at 20:01

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.