Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 收集2不';使用$push时无法清除数据_Meteor_Simple Schema_Meteor Collection2 - Fatal编程技术网

Meteor 收集2不';使用$push时无法清除数据

Meteor 收集2不';使用$push时无法清除数据,meteor,simple-schema,meteor-collection2,Meteor,Simple Schema,Meteor Collection2,我正在使用collection2将表单中的数据插入SimpleSchema。Collection2应该在插入之前清理数据,但是,如果使用$push插入数组,则不会清理数据。所有没有数据的字段都包含“。据我所知,不可能使用$set来清理数据。我错过了什么 代码片段:Schema const ProfileCandidateSchema = new SimpleSchema({ careerHistoryPositions: { type: Array, optional: true },

我正在使用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
      }]
    }
  });
});

当我使用带方括号的$set时,它会插入
careerHistoryPositions:[]
,而不插入其他内容。没有控制台错误或服务器错误。有什么想法吗?感觉它没有通过验证。Collection2说它可以是服务器端或客户端。它必须在方法中运行吗?不,它在
.insert()
.update()
等时运行。。。在任何地方都被称为。