我正在研究流星项目(0.8.2)并且有一个带有{name,address}字段的“addressDB”集合。当我尝试检索“addressDB”集合的记录时。它使用:addressDB.find({})给出了正确的结果;但我只需要获取登录流星应用程序的用户的记录。但登录后会提供所有记录。 我的HTML代码:
<div class="newCenters">
New records given are:<br>
{{#each entries}}
<strong>Added By: {{user.username}}</strong><br>
<strong> Address: </strong><i>{{address}}</i><br>
{{/each}}
</div>
我的js代码是:
Template.entries.entries = function () {
// return all entries sorted by time
return addressDB.find({}, { sort: { time: -1 }});// I assume change is required here
}
我在这段代码中插入数据:
var user = Meteor.user();
if (!user) {
return;
}
addressDB.insert({
user:user,// this inserts logged user's username
address:temp});
console.log("data inserted");
有什么想法吗?
答案 0 :(得分:0)
您需要添加一个选择器来指示您想要的记录。由于您要存储整个用户对象,因此您需要将用户的ID与user._id
文档中的addressDB
相匹配。可以使用Meteor.userId()
找到当前用户ID。把所有这些放在一起给了我们:
return addressDB.find({'user._id': Meteor.userId()}, {sort: { time: -1 }});
我怀疑这将解决您的迫切需求,但是我不建议您在初始化数据时存储整个用户对象。当您需要的只是id时,它将最终存储用户的个人资料,用户名等内容。相反,我会这样做:
var userId = Meteor.userId();
if (!userId)
return;
addressDB.insert({userId: userId, address:temp});
然后您的模板查询可能如下所示:
return addressDB.find({userId: Meteor.userId()}, {sort: { time: -1 }});