我对Meteor(和网络编程)非常陌生,并设置了两个集合,一个叫做Posts,另一个是作者(我确实知道我可以将所有这些信息放在一个集合中,但我想以这种方式尝试)。我正在尝试显示所有帖子,所以我在HTML代码中执行eachPost,这将遍历我的所有帖子。当我循环浏览我的帖子时,我试图将帖子ID保存在隐藏输入中,这样我就可以使用每个帖子的id来查询作者集合并显示作者姓名。我添加了一个console.log,它每次为我的postId编写undefined - 任何想法为什么这样做或更好的方法来解决这个问题(没有在帖子中嵌入作者信息)?
HTML:
<template name="dashboard">
{{#each eachPost}}
<input type="hidden" id="postId" value="{{_id}}">
<p>{{authorName}}</p>
{{/each}}
</template>
JS:
Template.dashboard.helpers({
eachPost: function()
{
return Posts.find({});
},
authorName: function()
{
var postId = $('#postId').val();
console.log(postId);
return Authors.findOne({_id: postId});
}
});
在此先感谢,我非常感谢任何帮助!
答案 0 :(得分:0)
我认为没有必要将其存储在隐藏元素中
您可以使用this._id
帮助
authorname
来访问它
你可以这样做
<template name="dashboard">
{{#each eachPost}}
<p>{{authorName}}</p>
{{/each}}
</template>
Template.dashboard.helpers({
eachPost: function()
{
return Posts.find({});
},
authorName: function()
{
console.log(this._id);
return Authors.findOne({_id: this._id});
}
});
试试这个,它可能适合你