Cos
Cos

Reputation: 1709

How do I trigger AutoValue?

I have two autovalue fields in my Collection. I wrote a Cronjob that loops through some of my collection items every half an hour. Basically I want to trigger/refresh the autovalues of those items without updating any other fields.

I tried using

MyCollection.simpleSchema().clean {
       extendAutoValueContext: {
           id: myCollection._id
       }
});

and I see all the console logs of the autovalue fields, right before returning the calculated value. The only problem is, the field value for one the values doesn't change. I see it in the logs, but on the DB it stays the same.

What is the correct way of refreshing AutoValue fields without necessarily updating other fields?

Edit: It seems cleaning leads to the second autovalue reverting the first.

How do I fix this?

Edit2: Here is my code:

score: {
    type: Number,
    autoValue: function(doc) {
        if (this.isInsert) {
          return 0;
        } 
        else {
            var maxValue = 0;
            Collections.Comments.find({ debateId: this.docId }).map(function(mapDoc){
                maxValue += mapDoc.votes;
            });
            console.log("Updated debate "+this.docId+" score to "+maxValue);
            console.log("debate score autovalue: "+JSON.stringify(doc, null, 4));
            return maxValue;
        }
    }
},
rank: {
    type: Number,
    decimal:true,
    optional: true,
    autoValue: function(doc){
        if(this.isInsert){
            return 0;
        }
        else {
            var newRank= someCalc();
            console.log("Updated debate "+this.docId+" rank to "+newRank+", score: "+s);
            console.log("debate rank autovalue: "+JSON.stringify(doc, null, 4));
            console.log("---------------------------");
            return newRank;
        }
    }
},

And here is an screenshot of my console output:

Console Logs

Now, the screenshot looks as if the score value has been accepted as "4". The problem, however, is that the Database still only shows "1".

Upvotes: 2

Views: 80

Answers (0)

Related Questions