Radko
Radko

Reputation: 131

Meteor - collection2 autoValue - how to access other document attributes that are not within the modifier

Let's say I'm generating an autoValue for an attribute of a document that I'm updating. However, I need one (or more) of the attributes of the existing document, which I don't have access to in the autoValue function.

For example: I am generating an autoIncrement value for the document. I need the doc.company and doc.date attributes to calculate it. However, I'm only updating, let's say the doc.isFinished attribute. Thus, the doc.date is not accessible via this.field().

Thanks a lot! :)

Upvotes: 1

Views: 894

Answers (1)

richsilv
richsilv

Reputation: 8013

This is exactly what the this.field method is for, but note that you should be passing field to it, not doc.field. For example:

YourCollectionSchema = new SimpleSchema({
    ...
    autoIncrement: {
        type: Number,
        autoValue: function() {
            var company = this.field('company').value,
                data = this.field('data').value;
            return // WHATEVER YOU NEED TO CALCULATE THE VALUE OF autoIncrement
        }
    },
    ...
});

Obviously this might not fit your exact requirement, but should demonstrate how you get the value of other fields in autoValue. If this kind of setup doesn't work for you then please paste your code, because it works perfectly for me.

Upvotes: 3

Related Questions