我仍在努力理解如何从另一个集合查询中将 Meteor.users 作为外键访问。我知道默认情况下仅发布当前用户,因此我在服务器上有一个发布为
Meteor.publish('itemOwner', function(userId) {
check(userId, String);
var user = Meteor.users.find({id: userId});
return user;
// return Meteor.users.find({id: userId}, {
// fields: {'profile': 1} });
});
然后我在客户端上有一个 Deps.autorun ..
Deps.autorun(function () {
var itemOwnerId = Session.get("itemOwnerID");
if (itemOwnerId) {
debugger
var ID = Session.get("itemOwnerID");
Meteor.subscribe('itemOwner', Session.get("itemOwnerID"));
}
});
我在模态表单加载上设置 session ID,并通过调用ownerProfile helper(或尝试)在模板中显示它
Template.showQuoteModalInner.helpers({
getQuote: function () {
// Get the quote ID from the session var
var quote = Session.get("quoteID");
if(quote) {
debugger;
var ID = quote.user._id;
Session.set("itemOwnerID", quote.user._id);
return quote;
}
},
ownerProfile: function() {
debugger;
var quote = Session.get("quoteID");
if(quote) {
var ID = quote.user._id;
var theUser = Meteor.users.find({_id: quote.user._id});
return theUser;
};
}
});
现在,我可以跟踪每个阶段的用户 ID,并查看它是否正确传递给自动运行和帮助程序。如果我在ownerProfile帮助程序中的调试器处停止程序,并在控制台中输入Meteor.user.fetch({_id: "the id here"}).fetch(),我会得到正确的用户..但是,在处理程序本身 Meteor.users.find 返回 null ???我错过了什么?
最佳答案
我注意到两种可能性。
首先,您在发布函数的查找中缺少下划线。
.find({id: userId})
应为 .find({_id: userId})
。
但如果您在控制台中看到用户(登录用户除外),这可能不是问题。
其次,如果您没有从 Template.showQuoteModalInner.ownerProfile
帮助程序中看到用户,可能是因为您返回的是 find() 而不是 findOne()。
find() 返回游标,而 findOne() 返回记录。如果您想显示单个用户的属性,请尝试 findOne()。
关于javascript - Meteor Users 集合和 Deps.autorun 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24712907/