【发布时间】:2015-10-02 13:30:19
【问题描述】:
在我的 \server\publications.js 文件中,我有这样的代码:
Meteor.publish("jobLocations", function () {
var currentUserId = this.userId;
return JobLocations.find({createdBy: currentUserId});
});
Meteor.publish("workers", function () {
var currentUserId = this.userId;
return Workers.find({createdBy: currentUserId});
});
. . .
IOW,我在每个发布方法中都使用了一个名为“currentUserId”的本地变量。
最好改成这样:
var currentUserId = null;
Meteor.publish("jobLocations", function () {
currentUserId = this.userId;
return JobLocations.find({createdBy: currentUserId});
});
Meteor.publish("workers", function () {
currentUserId = this.userId;
return Workers.find({createdBy: currentUserId});
});
...或者每个发布方法都需要自己的本地“currentUserId”变量是否有某些原因?
【问题讨论】:
-
你不需要使用var,你可以在需要的地方使用
this.userId.. -
好点;我只是使用“Your First Meteor App”中的示例代码,并没有用批判的眼光看待它。
标签: meteor global-variables publish-subscribe