Mongoose Schema Error: “Cast to string failed for value” when pushing object to empty array

前端 未结 5 1599
清酒与你
清酒与你 2020-12-17 15:32

I have a strange problem and cannot figure out what the problem is. The Error-message doesn\'t help.

I\'m sending an "alarm" to the server and want to save

5条回答
  •  清酒与你
    2020-12-17 16:00

    I would declare alarm as its own schema and set the alarms property as an array of alarm aka subdocuments. This will allow you to add validation to the alarm schema, etc. NOTE: Subdocuments don't get saved until the parent is saved.

    var alarmSchema = new Schema({
            timestamp : Number,
            dateTime : String, //yyyymmddhhss
            difference : Number,
            actionTaken : String, //"send sms"
    
    });
    
    var deviceSchema = new Schema({
       deviceId: {
            type : String,
            index : {
                unique : true,
                dropDups : true
            }
        },
        alarms : [alarmSchema]
    });
    

提交回复
热议问题