我有一个如下所示的车把片段
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Message</th>
<th scope="col">Time</th>
</tr>
</thead>
<tbody>
{{#each result}}
<tr>
<th scope="row">{{@index}}</th>
<td>{{this.name}}</td>
</tr>
{{/each}}
</tbody>
</table>
结果是来自 mongodb 的数组,如下所示
[
{
_id: new ObjectId("651de76a44d50f1d9cfedede"),
name: 'Lucy',
message: 'I way',
createdAt: 2023-10-04T22:30:02.377Z,
updatedAt: 2023-10-04T22:30:02.377Z,
__v: 0
},
{
_id: new ObjectId("651de7eca2da2627f94ce3cb"),
name: 'Tanya',
message: 'I like chemical',
createdAt: 2023-10-04T22:32:12.465Z,
updatedAt: 2023-10-04T22:32:12.465Z,
__v: 0
},
{
_id: new ObjectId("651de8854ee4b125468e6921"),
name: 'Otto',
message: 'I from hightower',
createdAt: 2023-10-04T22:34:45.179Z,
updatedAt: 2023-10-04T22:34:45.179Z,
__v: 0
}
]
如果我用 {{this}} 替换 {{this.name}} 它会显示
{
_id: new ObjectId("651de76a44d50f1d9cfedede"),
name: 'Lucy',
message: 'I way',
createdAt: 2023-10-04T22:30:02.377Z,
updatedAt: 2023-10-04T22:30:02.377Z,
__v: 0
}
用 {{name}} 替换 {{this.name}} 也不起作用(该块将保持空白)
这看起来与 how-to-iterate-over-array-of-objects-in-handlebars 非常相似,但简而言之,只是丢失
this.
- 它隐含为 #each
的一部分(尽管 文档根本没有说明这一点):
{{#each result}}
<tr>
<th scope="row">{{@index}}</th>
<td>{{name}}</td>
</tr>
{{/each}}