使用 $push 时 Collection2 不清理数据

Collection2 doesn't clean data when using $push

我正在使用 collection2 将表单中的数据插入到我的 SimpleSchema 中。 Collection2 应该在插入之前清理数据,但是,如果我使用 $push 插入数组,则不会清理数据。所有没有数据的字段都包含 ""。据我所知,不可能使用 $set 来清理数据。我错过了什么?

片段: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
  }
});

片段:Update

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

您可以用 $set 初始化数组,方法是在要插入的对象周围使用方括号:

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