我也看到了类似的问题,但未能实现。原谅我的不足。我正在尝试在脚本标签的前端使用对象数组。我的后端是节点和数据库猫鼬。前端,我使用了带有原始js的车把模板。我的后端代码是
UnitType.find()
.where({ type: type })
.select("-_id")
.exec()
.then(doc=> {
var unit_types = doc;
res.render('single-unit', {
unit_types: unit_types,
});
});
前端,我使用了像这样的unit_types-
{{#each unit_types}}
<tr>
<td width="40%">{{this.name_plural}}</td>
<td width="60%">{{this.name_singular}}</td>
</tr>
{{/each}}
在脚本标签中-
<script>
var unit_types = [{{unit_types}}] // [{{{unit_types}}}]
<script>
我使用脚本来操纵数据。我的数据就像-
[
{name_singular:"foot",name_plural:"feet"},
{name_singular:"yard",name_plural:"yards"},
..... // etc.
]
哪个工作正常。问题我面临两个问题,一个问题是如果我从查询中提取_id
,则会在<script>
内引发语法错误。第二,我正在使用一个函数使每个属性的首字母大写。像这样在后端-
var unittypes =doc.map(v => {
return {
name_singular_up: ucfirst(v.name_singular),
name_singular: v.name_singular,
name_plural_up: ucfirst(v.name_plural),
name_plural: v.name_plural,
}
如果我这样发送,则在<script>
内再次引发语法错误,在chrome元素中显示[Object object]
。但是模板中的数据可以正常工作。我找到了使用ajax.get
的工作方法。但是我本来希望不使用ajax
来做到这一点。我在这里做错了什么?
预先感谢。