I'm using collection2 to insert data from my form into my SimpleSchema. Collection2 is supposed to clean the data before insert, however, if I use $push to insert an array, the data isn't cleaned. All fields with no data contain ""
. As far as I'm aware it isn't possible to use $set which does clean the data. What am I missing?
Snippet: 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
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:
const updatePosition = this.state.careerHistoryPositions.map((position) => {
ProfileCandidate.update(this.state.profileCandidateCollectionId, {
$set: {
careerHistoryPositions: [{
uniqueId: position.uniqueId,
company: position.company
}]
}
});
});