【发布时间】:2012-06-24 18:22:12
【问题描述】:
我最近开始尝试流星的auth 分支,根据我调用调用 this.userId() 的流星方法的位置,它将返回 null 或我需要的用户 ID。
更具体地说,当我从 Meteor.startup 内部初始化的咖啡脚本类调用我的流星方法时,它可以工作,但当从 Meteor.publish 内部调用相同的方法时不起作用。
meteor 方法很简单,可能不相关,但以防万一:
Meteor.methods(
get_user_id: ->
return @userId()
)
编辑:似乎人们无法重现我的问题,这里是 todo auth 示例的一个补丁,应该可以演示它。
diff --git a/examples/todos/server/methods.js b/examples/todos/server/methods.js
index e69de29..d4182a6 100644
--- a/examples/todos/server/methods.js
+++ b/examples/todos/server/methods.js
@@ -0,0 +1,6 @@
+Meteor.methods({
+ get_user_id: function() {
+ console.log(this.userId());
+ return this.userId();
+ }
+});
diff --git a/examples/todos/server/publish.js b/examples/todos/server/publish.js
index 1552c5e..87cb29f 100644
--- a/examples/todos/server/publish.js
+++ b/examples/todos/server/publish.js
@@ -16,6 +16,8 @@ Todos = new Meteor.Collection("todos");
// Publish visible items for requested list_id.
Meteor.publish('todos', function (list_id) {
+ Meteor.call('get_user_id');
+ //console.log(this.userId())
return Todos.find({
list_id: list_id,
privateTo: {
感谢Eitan 的补丁!
【问题讨论】:
-
我不确定
Meteor.call是否打算在服务器端使用?我不确定,但这可能是这里的问题。 -
@TomColeman -- 如果您不相信我,请查看documentation。
-
是的,我想这是一个错误。我想你应该在 github 上打开一个问题并让开发人员知道。显然,此时 auth 分支是未发布的代码...
-
太好了,谢谢!我只是想在打开问题之前确定一下。
标签: meteor