我已将此数据插入到 mongo 中
db.orders.insert( { _id: ObjectId().str, name: "admin", status: "online",catalog : [
{
"objectid" : ObjectId().str,
"message" : "sold",
"status" : "open"
}
]})
我正在以这种方式访问数据
<template name="Listed">
<div class="row">
{{#each list}}
<article class="post">
<a href="{{pathFor route='edit'}}"><h3>{{_id}}</h3></a>
<a href="{{pathFor route='edit'}}"><h3>{{name}}</h3></a>
<br>
<a href="{{pathFor route='create'}}"><h3>{{status}}</h3></a>
<br>
{{#each catalog }}
<a href="{{pathFor route='create'}}"><h3></h3></a>
<a href="{{pathFor route='create'}}"><h3>{{status}}</h3></a>
{{/each}}
<div class="well"></div>
<br/>
</article>
<br/><br/>
{{/each}}
</div>
</template>
我有兴趣了解 catalog
对象的键/值对。
这是因为,我不知道 catalog
的字段。为此,我注册了一个 helper
Template.registerHelper("keyval",function(object){
return _.map(object, function(value, key) {
return {
key: key,
value: value
};
});
});
并以这种方式使用它
<template name="Listed">
<div class="row">
{{#each list}}
<article class="post">
<a href="{{pathFor route='edit'}}"><h3>{{_id}}</h3></a>
<a href="{{pathFor route='edit'}}"><h3>{{name}}</h3></a>
<br>
<a href="{{pathFor route='create'}}"><h3>{{status}}</h3></a>
<br>
{{#each keyval catalog }}
<a href="{{pathFor route='create'}}"><h3></h3></a>
<a href="{{pathFor route='create'}}"><h3>{{key}}</h3></a>
<a href="{{pathFor route='create'}}"><h3>{{value}}</h3></a>
{{/each}}
<div class="well"></div>
<br/>
</article>
<br/><br/>
{{/each}}
</div>
</template>
当我尝试访问像 {{key}}
这样的键时,我得到 0,1,2... 而 {{value}}
给出了一个对象。
这不是我要找的。如何正确显示键值对?
最佳答案
您正在生成一个数组数组(每个目录项映射到一个键/值对列表)。一种解决方案是遍历每个目录项,然后对其调用 keyval
。结构看起来像这样:
{{#each item in catalog}}
{{#each keyval item}}
<a href="{{pathFor route='create'}}"><h3>{{key}}</h3></a>
<a href="{{pathFor route='create'}}"><h3>{{value}}</h3></a>
{{/each}}
{{/each}}
关于javascript - 列出 blaze 对象的所有键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36408682/