1

假设我正在为正在更新的文档的属性生成一个 autoValue。但是,我需要现有文档的一个(或多个)属性,而我无法在 autoValue 函数中访问这些属性。

例如:我正在为文档生成一个 autoIncrement 值。我需要 doc.company 和 doc.date 属性来计算它。但是,我只是在更新,比如说 doc.isFinished 属性。因此,无法通过 this.field() 访问 doc.date。

非常感谢!:)

4

1 回答 1

3

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.

于 2014-11-01T18:04:06.643 回答