0

添加到集合对象中的对象中的数组会删除该对象中的所有其他数组。

考虑事件集合的成员:

{
        "_id" : "EfEq7niEyLLatb7fb",
        "attendeeFavorites" : {
                "mRRYzNBaFEmuqCrLi" : [ ],
                "jbm8BJJ3PJCyWRJwz" : [ ],
                "9Ze5M6CkHdLwkJdbu" : [ ],
                "bH5q4himZawTTrbyc" : [ ]
        }
}

的键attendeeFavorites是用户 ID。当该用户登录时,他们可以将字符串添加到相应的数组中。这是它的活动代码:

$meteor.autorun($scope, function () {

    var event = $scope.$meteorObject(Events, {}).subscribe('events');

    if( event.attendeeFavorites && event.attendeeFavorites[Meteor.userId()] ) {
        $scope.favorites = event.attendeeFavorites[Meteor.userId()];
    }
});

$scope.addFavorite = function(){
    $scope.favorites.push("x");
};

和发布功能:

Meteor.publish('events', function(){
    var projection = {
        name: 1
    };
    projection["attendeeFavorites."+this.userId] = 1;
    return Events.find({},{fields: projection});
});

例如,当用户9Ze5M6CkHdLwkJdbu调用时addFavorite(),会将 anx添加到他们的attendeeFavorites数组中,但它会删除所有其他条目。这把上面变成了:

{
        "_id" : "EfEq7niEyLLatb7fb",
        "attendeeFavorites" : {
                "9Ze5M6CkHdLwkJdbu" : [
                        "x"
                ]
        }
}

为什么会这样??

* 编辑 * 修改 发布功能

Meteor.publish('events', function(){
    return Events.find({});
});

修复它,但这不是一个真正的解决方案,因为我不希望用户能够查看其他用户的收藏夹数组。

4

1 回答 1

0

创建用户特定的订阅:

Meteor.publish('user_favorites').then(function() {
    return Events.find({attendeeFavorites: this.userId})
});

这将只返回该与会者的收藏夹

然后当你订阅它时:

$scope.favorites = $scope.$meteorCollection(Events).subscribe('user_favorites');

但是,如果您阅读 angular-meteor 文档,最好使用 $scope.$meteorSubscribe:

$scope.$meteorSubscribe('user_favorites', function() {
    $scope.$meteorCollection(Events, false); // false will prevent it from updating the db automatically. remove if you do want it to update
});

$scope.$meteorSubscribe 将在作用域被销毁时自动终止订阅。

请记住,我快睡着了,如果这还不满意,那么我会在早上修复它:)

编辑 - 我错过了你想要做的事情,所以试试这个:

在偶数集合中,您按 id 保留与会者列表:

{
    attendees: ['afasd89as8d923', 'q23rqwasdfj23', '..']
}

然后,每个用户都可以包含一组带有相关收藏夹的参加事件:

[
    {eventId: 'asdfasdf22q39f8', favorites: ['1', '2', '3']},
    {eventId: 'as2234assdf8989', favorites: ['asdf', 'foo', '3']}
]

我不知道你如何为你的最爱建模,但这并不重要。每次用户访问他/她已经可以访问他们自己的收藏夹的页面时,您只需显示与该事件关联的收藏夹即可。这当然是如果您对每个事件有不同的喜好。

确实,您不需要在每个活动中保留参加者的列表,但这对于其他目的可能会派上用场。

于 2015-09-05T03:24:47.480 回答