我一直在试图从Meteor.methods通话中访问this.userId
变量调用的时候,但它似乎不工作时,我尝试通过调用该方法不工作Meteor.setTimeout或Meteor.setInterval。访问this.userId从内Meteor.SetTimeout
这是我的本钱:
if (Meteor.is_server) {
Meteor.methods({
getAccessToken : function() {
try {
console.log(this.userId);
return Meteor.users.findOne({_id: this.userId}).services.facebook.accessToken;
} catch(e) {
return null;
}
}
});
var fetch_feed = function() {
console.log(Meteor.call("getAccessToken"));
[...] // A bunch of other code
};
Meteor.startup(function() {
Meteor.setInterval(fetch_feed, 60000); // fetch a facebook group feed every minute
Meteor.setTimeout(fetch_feed, 3000); // initially fetch the feed after 3 seconds
});
}
看终端的日志中,this.userId
总是返回空。但是,如果我尝试从客户端或通过控制台调用方法,它将返回正确的ID。
如何走到这一步不从Meteor.setInterval内工作?这是一个错误还是我做错了什么?
“this.userId”指向“null”还是“getAccessToken”返回null,因为您捕获了异常并强制它? – Rahul 2013-03-11 15:26:52
来自'getAccessToken'的错误是'TypeError:无法读取undefined'的属性'services',因为'this.userId'返回'null'。如果我从控制台调用该方法,它将起作用,但是从Meteor.setTimeout或Meteor.setInterval失败。 – 2013-03-11 16:20:57
顺便说一句。如果我将setTimeout和setInterval移到客户端,这可以正常工作。就像this.userId在服务器端调用时不可用。但基于文档,它应该可以在任何地方使用:http://docs.meteor.com/#method_userId – 2013-03-11 21:31:27