【发布时间】:2020-07-30 00:25:23
【问题描述】:
目前正在使用 mongoDB 的 $filter aggregation,它已经能够从我的查询中产生预期的输出。问题出在我的 HTML 中,而 *ngFor 没有显示我的数据,也没有在控制台中显示任何错误。
为了更好地理解,这是我从 console.log 生成的 JSON:
下面是我的查询代码:
//api.js
router.get('/agent-policies/:name', (req, res) => {
db.collection('users').aggregate([
// Get just the docs that contain an agent element where agent is === req.params.name
{$match: {'paPolicies.policy.agent': req.params.name}},
{$project: {
policy: {$filter: {
input: '$paPolicies.policy',
as: 'police',
cond: {$eq: ['$$police.agent', req.params.name]}
}},
_id: 0
}}
]).toArray((err, results) => {
if (err) throw err;
res.send(results);
})
})
最后,我的模板代码:
//component.html
<div *ngFor='let police of agentPolicies'>
{{police.relationship}}
</div>
agentPolicies 在我的component.ts 中被设置为数组:
agentPolicies: any = [];
由于代码没有产生任何错误,我认为问题出在我的 HTML 中,并且我没有正确使用 *ngFor。为了从我的 JSON 中显示 relationship 的值,我需要进行哪些更改?
【问题讨论】:
标签: node.js angular mongodb typescript express