我有一个返回结果的节点查询。但是,我视图中的把手帮助器将整个阵列视为一个结果:
{{#if donations}}
{{#each donations}}
{{this}} is printing the entire set of donations!
{{/each}}
{{/if}}
{{this}}
打印整个数组,而不是只在网页上打印一个对象:
{ 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 },
以下是相关的路由器代码:
// 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});
})
});
});
这是它引用的模型函数:
module.exports.getDonationsByUserId = function(id, callback){
return Donation.find({owner: id}, function(err, donations) {
//ar query = {owner: id};
return callback(err, donations);
});
}
如何迭代每个单独的对象结果?
谢谢!
答案 0 :(得分:1)
考虑下面的JSON结构,
{
"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
}
]
}
您可以使用以下模板结构。
{{#if donations}
{{#each donations}}
{{#each this}}
{{this}}
{{/each}}
is printing the entire set of donations!
{{/each}}
{{/if}}
将输出打印为
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!
进行测试
希望这有帮助。