0

我正在用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 年的“You First Meteor Application”一书。所以也许他们在哪里发生了重大变化……或者我错过了一些东西。

我是 Meteor 的新手,因此我们将不胜感激有关该问题的任何信息。

4

1 回答 1

2

在您的代码中,您有:

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

this如果你想被调用者驱动,不要使用箭头函数!而是这样做:

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

更多的

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

于 2016-04-28T05:47:05.927 回答