I wrote this schema:const mongoose = require('mongoose'); const DetailsSchema = new mongoose.Schema({ _id: String, name: String, value: Number }, {_id: false}); const Details = mongoose.model('details', DetailsSchema); module.exports = Details; I want to receive the value for the id in an http.post so i wrote this:router.post('/api/newSolicitation', async (req, res) => { const { value, id, name } = req.body; const checkforid = await Details.findOne({id}); if(checkforid) { const result = await Details.save(); console.log('Detail updated: ', result); res.json({ success: true, message: 'Detail Updated' }); } else { const detail = new Details({ id, name, value }); const submit = await detail.save(); console.log('New detail created:', submit); res.json({ success: true, message: 'New detail created!' }); } }); When i try to test the else by creating a new detail i receive this error:UnhandledPromiseRejectionWarning: MongooseError: document must have an _id before saving I get it i must declare the id value but I'm doing that when i create the detail constant. Can somebody help see what am i missing here? Thanks a lot
Submitted June 16, 2019 at 02:46PM by DarthBit
No comments:
Post a Comment