I'm runing this on my meteor.js server:
我在meteor.js服务器上运行:
Meteor.publish('games', function(){
this.onStop(function() {
Meteor.call('leaveQueue');
});
return Games.find({ player: this.userId })
});
When the user stops the subscription, it calls this function that is on methods.js:
当用户停止订阅时,它会调用methods.js上的此函数:
Meteor.methods({
leaveQueue:function(){
console.log(this.userId);
}
});
It logs null as the userId.. Now if I call it from the frontend using Meteor.call('leaveQueue') on the console, it logs the user id correctly.
它将null记录为userId ..现在,如果我在控制台上使用Meteor.call('leaveQueue')从前端调用它,它会正确记录用户ID。
I even tried console.log(Meteor.userId) and console.log(Meteor.userId()), all null.
我甚至尝试过console.log(Meteor.userId)和console.log(Meteor.userId()),全部为null。
What could be going on?
会发生什么事?
1 个解决方案
#1
0
Meteor allows you to call a Method from another Method on the server side, and maintain the proper context (so userId
, connection
, etc. are all taken from the original Method call). This is not the case when calling a Method from a publish function however. When you make your Meteor.call
from within your publication, the called Method tries to extract userId
details from the current DDP connection (by looking at the internal DDP._CurrentInvocation
). These details don't exist, so the called Method is unable to preserve them (for more information, see the ddp-server/livedata_server.js source).
Meteor允许您从服务器端的另一个方法调用方法,并维护正确的上下文(因此userId,连接等都取自原始方法调用)。但是,从发布函数调用Method时不是这种情况。当您从发布中创建Meteor.call时,被调用的方法尝试从当前DDP连接中提取userId详细信息(通过查看内部DDP._CurrentInvocation)。这些细节不存在,因此被调用的Method无法保留它们(有关更多信息,请参阅ddp-server / livedata_server.js源)。
That being said, you are able to get the current userId
in the onStop
callback of your publication:
话虽这么说,您可以在出版物的onStop回调中获取当前的userId:
Meteor.publish('games', function games() {
this.onStop(() => {
// This will log the proper userId
console.log(this.userId);
});
return Games.find({ player: this.userId })
});
I'd suggest running your Method code with the userId
in the onStop
callback by calling a utility function instead of a Meteor Method. If you want to avoid duplicating code, you could extract the common code of your Method out into a utility function, and leverage it in both places. For example:
我建议通过调用实用程序函数而不是Meteor方法在onStop回调中使用userId运行Method代码。如果要避免重复代码,可以将方法的公共代码提取到实用程序函数中,并在两个位置使用它。例如:
// Stored in a utility file somewhere
function doSomethingCommon(userId) {
// Do something ...
}
Meteor.methods({
leaveQueue() {
doSomethingCommon(Meteor.userId());
}
});
Meteor.publish('games', function games() {
this.onStop(() => {
doSomethingCommon(this.userId);
});
return Games.find({ player: this.userId })
});
#1
0
Meteor allows you to call a Method from another Method on the server side, and maintain the proper context (so userId
, connection
, etc. are all taken from the original Method call). This is not the case when calling a Method from a publish function however. When you make your Meteor.call
from within your publication, the called Method tries to extract userId
details from the current DDP connection (by looking at the internal DDP._CurrentInvocation
). These details don't exist, so the called Method is unable to preserve them (for more information, see the ddp-server/livedata_server.js source).
Meteor允许您从服务器端的另一个方法调用方法,并维护正确的上下文(因此userId,连接等都取自原始方法调用)。但是,从发布函数调用Method时不是这种情况。当您从发布中创建Meteor.call时,被调用的方法尝试从当前DDP连接中提取userId详细信息(通过查看内部DDP._CurrentInvocation)。这些细节不存在,因此被调用的Method无法保留它们(有关更多信息,请参阅ddp-server / livedata_server.js源)。
That being said, you are able to get the current userId
in the onStop
callback of your publication:
话虽这么说,您可以在出版物的onStop回调中获取当前的userId:
Meteor.publish('games', function games() {
this.onStop(() => {
// This will log the proper userId
console.log(this.userId);
});
return Games.find({ player: this.userId })
});
I'd suggest running your Method code with the userId
in the onStop
callback by calling a utility function instead of a Meteor Method. If you want to avoid duplicating code, you could extract the common code of your Method out into a utility function, and leverage it in both places. For example:
我建议通过调用实用程序函数而不是Meteor方法在onStop回调中使用userId运行Method代码。如果要避免重复代码,可以将方法的公共代码提取到实用程序函数中,并在两个位置使用它。例如:
// Stored in a utility file somewhere
function doSomethingCommon(userId) {
// Do something ...
}
Meteor.methods({
leaveQueue() {
doSomethingCommon(Meteor.userId());
}
});
Meteor.publish('games', function games() {
this.onStop(() => {
doSomethingCommon(this.userId);
});
return Games.find({ player: this.userId })
});