Meteor Mongo.Cursor在Tracker.autorun中返回重复的行

时间:2016-09-30 11:20:14

标签: meteor typescript angular-meteor

我正在使用Tracker.autorun中的fetch()将Mongo.Cursor转换为数组并将其分配给songsArray。但每次更改基础数据库(被动)时,我都会在songsArray

中看到重复的值
private songsArray:Array<any>;
songsCursor:Mongo.Cursor<any>;

//...Some code here
ngOnInit():any {
//... Some code here

    this.songsCursor = Songs.find();
    Tracker.autorun(() => {
        this.songsArray = [];
        this.songsArray = this.songsCursor.fetch();
    });
}

为什么会发生这种情况,如果我认为我做错了,那么在Tracker.autorun中将游标转换为数组的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

在你的构造函数中,你需要做这样的事情:

    $reactive(this).attach($scope);

// Subscribe to collections here:
    this.subscribe('songs');

    this.helpers({
      songs: () => Songs.find()
    });

并且您会发现歌曲是您想要的数组