So I am currently trying to replace an array in an Element.
oldEvent.interests = ["test", "test2"];
console.log(oldEvent);
but my Element is not changed in the console.log. oldEvent.interests is an empty array and in the log it is still empty.
My model looks like this:
const eventSchema = new mongoose.Schema({
title: {
type: String, required: true, minlength: 2, maxlength: 128,
validate: {
validator: v => /^[\w\d öäüÖÄÜ]*$/.test(v),
message: props => `"${props.value}" may only consist of normal letters and numbers`
},
},
date: {type: Date, required: true},
description: {type: String, required: true, maxlength: 999},
maxParticipants: {type: Number, min: 0},
participants: [{type: mongoose.Schema.Types.ObjectId, ref: "User"}],
joinDeadline: {type: Date, required: true},
interests: {
type: [{type: mongoose.Schema.Types.ObjectId, ref: "Interest"}],
validate: {
validator: v => v.length <= 10,
message: props => `"${props.value}" may only consist of up to 10 interests`
},
},
host: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true},
location: {type: geoSchema}
});
I don't want to pop the array empty and then readd the new elements, is there a more elegant way ?
oldEvent.interests-Field, in the console.log it still is empty.oldEvent.set({ interest: ["test", "test2"] })andoldEvent.save()=does not work. If you want you can for that into an answer ;:)