HelloI am learning node and trying to set up a PUT method. Also I am using MongoDB and mongoose. Stuck on the syntax to push a new entry to an existing document with a nested array (maxLogSchema).Below is my schema:const maxLogSchema = new mongoose.Schema({entry: {type: String,required: true,},date: {type: String,required: true}})const Cardio = mongoose.model('Cardio', new mongoose.Schema({name: {type: String,required: true,minlength: 3,maxlength: 50},description: {type: String,required: true,minlength: 5,maxlength: 50},maxLog: [ maxLogSchema ]}))Below is my attempt at the PUT :router.put('/:id', async (req, res) => {const { error } = validate(req.body);if (error) return res.status(400).send(error.details[0].message);let id = req.params.id;const cardio = await Cardio.findByIdAndUpdate(id,[{"maxLog": [{"entry": "9000 min","date": "10-02-2019"}]}])if (!cardio) return res.status(404).send('The cardio with the given ID was not found');res.send(cardio);});I am getting an error that I need to add in a try catch, but also I don't know where to start with pushing my new data in the "maxLog" array.Any help would be great, thanks!
Submitted October 03, 2019 at 12:14PM by la712
No comments:
Post a Comment