把手和通过JSON对象的循环
介意我询问一些关于如何循环使用以下JSON对象并将其放入Handlebar模板的建议吗 我在这里发现了很多关于“把手和JSON循环”的类似问题,但在阅读了好几个小时并尝试了各种置换后,我被卡住了 如果打印{{resorts},resorts对象将显示在模板页面ok上 我可以得到一个项目清单,似乎与度假村内的文档数量相符 但我的目标是获得每个属性名称的项目符号列表,但我还没有做到这一点 非常感谢您的帮助 我的JSON:把手和通过JSON对象的循环,json,mongodb,mongoose,handlebars.js,express-handlebars,Json,Mongodb,Mongoose,Handlebars.js,Express Handlebars,介意我询问一些关于如何循环使用以下JSON对象并将其放入Handlebar模板的建议吗 我在这里发现了很多关于“把手和JSON循环”的类似问题,但在阅读了好几个小时并尝试了各种置换后,我被卡住了 如果打印{{resorts},resorts对象将显示在模板页面ok上 我可以得到一个项目清单,似乎与度假村内的文档数量相符 但我的目标是获得每个属性名称的项目符号列表,但我还没有做到这一点 非常感谢您的帮助 我的JSON: { properties: { Country: 'United
{ properties: {
Country: 'United States',
Name: 'Killington',
description: `blurb`
},
geometry: {
coordinates: [ -72.8032981, 43.6176027 ],
type: 'Point'
},
datePosted: 2020-01-10T12:07:00.340Z,
_id: 5e11cd71746ed55b54a760ec,
type: 'Feature'
},
{ properties: {
Country: 'United States',
Name: 'Jay',
description: `blurb`
},
geometry: {
coordinates: [ -72.8032981, 43.6176027 ],
type: 'Point'
},
datePosted: 2020-01-10T12:07:00.340Z,
_id: 5e11cd71746ed55b54a760ec,
type: 'Feature'
}
{{#if resorts}}
<ul>
{{#each resorts}}
<li>
{{properties.Name}}
</li>
{{/each}}
</ul>
{{/if}}
我的MongoDB模型(resort.js)
我的JS
exports.home = async(req, res) => {
const Resort = require('../models/resort')
const resorts = await Resort.find({})
res.render('home', {resorts: resorts})
}
我的把手模板:
{ properties: {
Country: 'United States',
Name: 'Killington',
description: `blurb`
},
geometry: {
coordinates: [ -72.8032981, 43.6176027 ],
type: 'Point'
},
datePosted: 2020-01-10T12:07:00.340Z,
_id: 5e11cd71746ed55b54a760ec,
type: 'Feature'
},
{ properties: {
Country: 'United States',
Name: 'Jay',
description: `blurb`
},
geometry: {
coordinates: [ -72.8032981, 43.6176027 ],
type: 'Point'
},
datePosted: 2020-01-10T12:07:00.340Z,
_id: 5e11cd71746ed55b54a760ec,
type: 'Feature'
}
{{#if resorts}}
<ul>
{{#each resorts}}
<li>
{{properties.Name}}
</li>
{{/each}}
</ul>
{{/if}}
更新
目前正在尝试在MongoDB中创建JSON验证。可能与此有关。
<ul>
{{#each resorts}}
{{#each this}}
<li>{{name}}</li>
{{/each}}
{{/each}}
</ul>
{{{每个度假村}
{{{#每个这个}
{{name}}
{{/每个}}
{{/每个}}
或
{{{每个度假村}
{{{#每个这个}
- {{this.name}}
{{/每个}}
{{/每个}}
{{{每个度假村}
{{{#每个这个}
- {{name}}
{{/每个}}
{{/每个}}
或
{{{每个度假村}
{{{#每个这个}
- {{this.name}}
{{/每个}}
{{/每个}}
您是否尝试将此
用作{{{this.properties.Name}}
?谢谢您的建议@chridam。这让我有了子弹。但不幸的是,这个名字没有传过来。其他类似的变体,如{{this.Name}}或{{{Name}},我也没有成功。您是否尝试过将this
用作{{this.properties.Name}}
?谢谢您的建议@chridam。这让我有了子弹。但不幸的是,这个名字没有传过来。对于其他类似的变体,例如{{this.Name}或{{Name},我也没有成功。
<ul>
{{#each resorts}}
{{#each this}}
<li>{{name}}</li>
{{/each}}
{{/each}}
</ul>
<ul>
{{#each resorts}}
{{#each this}}
<li>{{this.name}}</li>
{{/each}}
{{/each}}
</ul>