Meteor 如何在观察回调中获取用户ID

Meteor 如何在观察回调中获取用户ID,meteor,meteor-accounts,meteor-collections,meteor-useraccounts,Meteor,Meteor Accounts,Meteor Collections,Meteor Useraccounts,我想在服务器上对我的一个集合使用observe,但我需要获取userId, 我正在尝试使用this.userId和Meteor.userId()但不起作用!有关更多详细信息和错误消息,请参阅下面的代码 如何解决 Messages.find({state:"outbox"}).observe({ added: (doc) => { console.log(" observe "); console.log("userId : " + this.userId)

我想在服务器上对我的一个集合使用observe,但我需要获取userId, 我正在尝试使用
this.userId
Meteor.userId()
但不起作用!有关更多详细信息和错误消息,请参阅下面的代码 如何解决

Messages.find({state:"outbox"}).observe({
    added:  (doc) => {
    console.log(" observe ");
        console.log("userId : " + this.userId);  // undefined
        console.log("Meteor.userId(): " + Meteor.userId()); //  "Exception in queued task: Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions."
        //.......
   }
});

感谢您的关注。

observe
回调中,
this
关键字没有指向发布对象(它指向相关查询的光标),因此它没有
userId
属性

您可以创建一个闭包,使用

const userId=this.userId;

在发布本身的主体中,然后在回调中使用它(作为
userId
)。

这是从发布函数中调用的,还是在服务器启动时全局调用的?