Q:

Separate node js query results into objects?

I have a node query that is returning results. However, the handlebars helpers on my view is treating the entire array as one result:
  {{#if donations}}
     {{#each donations}}
        {{this}} is printing the entire set of donations!
     {{/each}}
  {{/if}}

The {{this}} prints the entire set of the array instead of just printing one object on the web page:
{ owner: '5a6d4c99d6320c05223e0cdb', _id: 5a930456d5ff0809409d15d8, name: 'test', price: 24, description: 'please donate to our cause', enddate: null, __v: 0 },{ owner: '5a6d4c99d6320c05223e0cdb', _id: 5a9a0b601be9210796083c9b, name: 'please donate again', price: 150, description: 'medical condition', enddate: null, __v: 0 },

Here's the relevant router code:
// GET user by username
router.get('/:username', function(req, res) {

  //var id = req.params.id;
  var username = req.params.username;

  User.getUserByUsername(username,function(err, user){
    const vm = user;
    var id = vm.id;

    Donation.getDonationsByUserId(id, function(err, donations){
      const vd = { donations };
      //console.log(vd);
      res.render('user', {donations: vd, user: vm});
    })
  });
});

And here's the model function it's referencing:
module.exports.getDonationsByUserId = function(id, callback){
  return Donation.find({owner: id}, function(err, donations) {
    //ar query = {owner: id};
    return callback(err, donations);
  });
}

How can I iterate through each individual object result?
Thanks!

A:

Considering your JSON structure as below,
{
  "donations": [
    {
      "owner": "5a6d4c99d6320c05223e0cdb",
      "_id": "5a930456d5ff0809409d15d8",
      "name": "test",
      "price": 24,
      "description": "please donate to our cause",
      "enddate": null,
      "__v": 0
    },
    {
      "owner": "5a6d4c99d6320c05223e0cdb",
      "_id": "5a9a0b601be9210796083c9b",
      "name": "please donate again",
      "price": 150,
      "description": "medical condition",
      "enddate": null,
      "__v": 0
    }
  ]
}

You may use the below template structure.
{{#if donations}
  {{#each donations}}
    {{#each this}}
      {{this}} 
    {{/each}}
    is printing the entire set of donations!
  {{/each}}
{{/if}}

Which prints the output as,
  5a6d4c99d6320c05223e0cdb 
  5a930456d5ff0809409d15d8 
  test 
  24 
  please donate to our cause 

  0 
is printing the entire set of donations!
  5a6d4c99d6320c05223e0cdb 
  5a9a0b601be9210796083c9b 
  please donate again 
  150 
  medical condition 

  0 
is printing the entire set of donations!

Tested using http://tryhandlebarsjs.com
Hope this helps.

