Meteor.users集合在更新

时间:2016-01-14 00:52:01

标签: mongodb meteor insert-update

我正在调用方法:upvoteProject

'upvoteProject'(projectId){
    check(projectId, String);

    // Update project
    let projectById = Projects.findOne({_id: projectId});
    Projects.update({_id: projectId}, {$push: {backers: this.userId}, $inc: {totalBackers: 1}});

    // Register user votes for project
    const FIELDS = {
        availableVotes: 1,
        latestVoteAt: 1,
        canVoteAgainAt: 1,
    };
    let user = Meteor.users.findOne({_id: this.userId}, {fields: FIELDS});
    let $set = {}, $push = {}, $inc = {};

    if (user['availableVotes'] <= 1) {
        $set['canVoteAgainAt'] = getDatePlusTime({days: DATE_LIMIT});
        $push['projectsBacked'] = projectId;
    } else {
        $set['latestVoteAt'] = new Date();
        $inc['availableVotes'] = -1;
    }

    console.log($set, $push, $inc); // { latestVoteAt: Wed Jan 13 2016 18:40:37 GMT-0600 (CST) } {} { availableVotes: -1 }

    Meteor.users.update({_id: this.userId}, {$set: $set}, {$inc: $inc, $push: $push}, function(error, docs){
        console.log(error, docs); // null 1
    });

虽然Projects.update效果很好,但我无法使Meteor.users.update()减少值,但文档确实受到影响(null 1)。

我尝试过不使用任何$set, $push, $inc个对象,而是像这样手动设置字段:

Meteor.users.update({_id: this.userId}, {$set: {fieldName: value} {//...}});

但也没有成功。

其他Meteor.users.update()方法正在运行。

1 个答案:

答案 0 :(得分:1)

查看文档,http://docs.meteor.com/#/full/update我看到您在单独的对象中应用了多个更新:

应该是

Collection.update({/ query /},{/ update object /},[options],callback)

所以试试:

Meteor.users.update({_id: this.userId}, {$set: $set, $inc: $inc, $push: $push}, function(error, docs){
        console.log(error, docs); // null 1
    });