如果他已登录,则发布给用户

时间:2016-04-28 05:45:38

标签: meteor typescript angular2-meteor

我正在使用accounts-password包编写meteor-angular2项目。

我试图找出用户是否已登录,如果是,则发布内容。

这是我的打字稿代码:

import {Meteor} from 'meteor/meteor';
import {RoomsCollection} from "../collections/rooms";

Meteor.publish('rooms',()=>{
    if (!this.userId) {
        return null;
    } else {
        return RoomsCollection.find();
    }
});

你可以看到我正在使用this.userId,但似乎this未定义。

这是错误:

TypeError: Cannot read property 'userId' of undefined

我不太了解匿名函数如何this.userId,我实际上正在读“你的第一个流星应用程序”这本书。从2014年开始。也许他们可能会发生变化......或者我错过了什么。

我是Meteor的新手,所以我们将非常感谢有关此问题的任何信息。

1 个答案:

答案 0 :(得分:2)

在您的代码中,您有:

()=>{
    if (!this.userId) {

如果您希望调用者驱动this,请不要使用箭头功能!而是做:

function() {
    if (!this.userId) {

更多

https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html