简体   繁体   English

使用$ push时Collection2不会清除数据

[英]Collection2 doesn't clean data when using $push

I'm using collection2 to insert data from my form into my SimpleSchema. 我正在使用collection2将表单中的数据插入到SimpleSchema中。 Collection2 is supposed to clean the data before insert, however, if I use $push to insert an array, the data isn't cleaned. Collection2应该在插入之前清除数据,但是,如果我使用$ push插入数​​组,则不会清除数据。 All fields with no data contain "" . 所有没有数据的字段都包含"" As far as I'm aware it isn't possible to use $set which does clean the data. 据我所知,不可能使用$ set清除数据。 What am I missing? 我想念什么?

Snippet: Schema 片段: Schema

const ProfileCandidateSchema = new SimpleSchema({
  careerHistoryPositions: { type: Array, optional: true },
  'careerHistoryPositions.$': { type: Object, optional: true },
  'careerHistoryPositions.$.uniqueId': { type: String, optional: true },
  'careerHistoryPositions.$.company': { 
    type: String, 
    optional: true, 
    custom: function () {
      const shouldBeRequired = this.field('careerHistoryPositions.company').value;

      if (!shouldBeRequired) {
        // updates
        if (this.isSet) {
          if (this.operator === '$set' && this.value === null || this.value === '') return SimpleSchema.ErrorTypes.REQUIRED;
        }
      }
    }  
  }
}, {
  clean: {
    filter: true,
    autoConvert: true,
    removeEmptyStrings: true,
    trimStrings: true,
    getAutoValues: true,
    removeNullsFromArrays: true
  }
});

Snippet: Update 片段: Update

 const updatePosition = this.state.careerHistoryPositions.map((position) => {
      ProfileCandidate.update({
        _id: this.state.profileCandidateCollectionId
      }, {
        $push: {
          'careerHistoryPositions': {
            uniqueId: position.uniqueId,
            company: position.company
          }
        }
      });
    });

You can initialize an array with $set by using square brackets around the object to be inserted: 您可以使用$set初始化数组,方法是在要插入的对象周围使用方括号:

const updatePosition = this.state.careerHistoryPositions.map((position) => {
  ProfileCandidate.update(this.state.profileCandidateCollectionId, {
    $set: {
      careerHistoryPositions: [{
        uniqueId: position.uniqueId,
        company: position.company
      }]
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM