2017-06-28 1 views
0

Ich verwende collection2, um Daten aus meinem Formular in mein SimpleSchema einzufügen. Collection2 soll die Daten vor dem Einfügen bereinigen. Wenn ich jedoch $ push verwende, um ein Array einzufügen, werden die Daten nicht bereinigt. Alle Felder ohne Daten enthalten "". Soweit mir bekannt ist, ist es nicht möglich $ set zu verwenden, was die Daten bereinigt. Was vermisse ich?

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

Schnipsel: Update

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

Antwort

0

ein Array mit $set durch Verwendung von eckigen Klammern um das Objekt initialisieren kann eingefügt:

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

Wenn ich Verwenden Sie $ set mit eckigen Klammern fügt es 'careerHistoryPositi ein ons: [] ', mit nichts anderem. Es gibt keine Konsolenfehler oder Serverfehler. Irgendwelche Ideen? – bp123

+1

Es fühlt sich an, als ob es die Validierung versagt. –

+0

Collection2 sagt, dass es Server- oder Client-Seite sein kann. Muss es in einer Methode laufen? – bp123

Verwandte Themen