Meteor:使用用户配置文件属性而不是 ID 发布

Meteor: Publish using users profile properties rather than ID

我目前正在创建一个将被多家公司使用的应用程序。 每个用户都有以下配置文件:

username: johnDoe    
emails: [{address: "some@email.com", verified: true}],
profile: {
             name: "John Doe",
             companyId: "1234"
}

然后我有一个公司对象集合(称为公司),其中包含特定于该公司的配置信息、模板等。

{
    id: "1234",
    configuration: {},
    templates: []
}

为了隔离每个公司的数据,我只想发布与用户配置文件 companyId 与公司 id 相匹配的数据。

if (Meteor.isServer) {
    // collection to store all customer accounts
    Companies = new Mongo.Collection('Companies');

    // publish collection
    Meteor.publish("Company", function () {
         return Companies.find({id: Meteor.user().profile.companyId});
    })
}

如果我对诊所 ID 进行硬编码,这目前有效

    // publish collection
    Meteor.publish("Company", function () {
         return Companies.find({id: "1234");
    })

但是 returns 一个带有 Meteor.user().profile.companyId 的空光标。 这意味着问题要么是我使用了错误的函数,要么更可能是发布发生在 user().profile.companyId 可以 运行.

之前

有人知道我做错了什么吗?您对阅读哪些内容有什么建议,以便我了解这一进展?

谢谢

尝试在发布函数中执行显式 findOne():

// publish collection
Meteor.publish("Company", function () {
     var user = Meteor.users.findOne({_id: this.userId});
     if(user && user.profile && user.profile.companyId) {
       return Companies.find({id: user.profile.companyId});
     } else {
       console.log(user);
       return this.ready();
     }
});