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. Commented 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. Commented Sep 10, 2017 at 19:44
  • 1
    @Keith please help me with it?how to do it? Commented Sep 10, 2017 at 19:45
  • @BrianDriscoll ya it is initialized Commented 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. Commented 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 );
Sign up to request clarification or add additional context in comments.

6 Comments

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
nice answer! @DuncanThacker
You need to get each index and the value then write it to the response @RadhikaObhan check my update
If you don't want JSON, why are you using res.json()?
@DuncanThacker what alternate can I use to display only key and value data?
|
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 Comments

can we see your code? where does the res go to? @RadhikaObhan
@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.
true, I just saw the mistake there, gonna edit it in a sec @alexmac

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.