I'm working with Meteor and want to reach values that are stored in a field which functions as an internal array.
我正在使用Meteor,并希望获得存储在作为内部数组的字段中的值。
When I run the query (with projection) I get one record which looks like that:
当我运行查询(带投影)时,我得到一条看起来像这样的记录:
{ "comments" : [ { "uid" : "1", "un" : "Sarah", "c" : "cc" }, { "uid" : "2", "un" : "Leo", "c" : "dd" } ] }
I need to show in a template "un" and "c" of each record in the array. I tried that:
我需要在数组中的每个记录的模板“un”和“c”中显示。我试过了:
html:
<template name="allComments">
<ul>
{{#each allC}}
<li>{{un}}</li>
{{/each}}
</ul>
</template>
js:
Template.allComments.allC = function () {
//query that returns result as above
}
I've tried also {{#with}}
, nested {{#each}}
and nested templates but it doesn't work..
我也尝试了{{#with}},嵌套{{#each}}和嵌套模板,但它不起作用..
How can I reach this value?
我怎样才能达到这个值?
Thanks a lot, Sarah.
非常感谢,莎拉。
2 个解决方案
#1
0
Try changing your JS:
尝试更改你的JS:
Template.allComments.helpers({
allC: function() {
//query that returns result as above
}
});
'#each' should then work in your template.
然后'#each'应该在你的模板中工作。
#2
0
I finally managed to show this comments with:
我终于设法用这样的评论来表达:
Template.allComments.helpers({
allC: function () {
var result=[];
TasksList.findOne({_id:Session.get('selectedID')})['comments'].forEach(function(entry){
result.push(entry['un']+entry['c']);
});
return result;
},});
and
<template name="allComments">
<ul>
{{#each allC}}
<li>{{this}}</li>
{{/each}}
</ul>
#1
0
Try changing your JS:
尝试更改你的JS:
Template.allComments.helpers({
allC: function() {
//query that returns result as above
}
});
'#each' should then work in your template.
然后'#each'应该在你的模板中工作。
#2
0
I finally managed to show this comments with:
我终于设法用这样的评论来表达:
Template.allComments.helpers({
allC: function () {
var result=[];
TasksList.findOne({_id:Session.get('selectedID')})['comments'].forEach(function(entry){
result.push(entry['un']+entry['c']);
});
return result;
},});
and
<template name="allComments">
<ul>
{{#each allC}}
<li>{{this}}</li>
{{/each}}
</ul>