By saving data from the postman I want to get a response from MongoDB include newly created document
Codes
async save(req, res){
const {score, multiplier, level} = req.body;
const scores = new Result({score, multiplier, level});
const savedRes = await scores.save().then(resp=>{
return resp
.sendStatus(200)
.send({
message:'new record is saved',
data: savedRes
})
}).catch(error=>res.sendStatus(500).send(error))
}
mongo configuration
const app = express()
mongoose.connect(process.env.MONGODB_URL,{
useNewUrlParser: true,
useUnifiedTopology: true,
})
const con= mongoose.connection
try{
con.on('open',() => console.log('db connected'))
} catch(error){
console.log(error)
}
app.use(cors({origin:"*"}))
Actual result
- Document is created in MongoDB Atlas with
internal server errorinpostmanreponse body along with(node:96318) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the clientin vscode terminal
Expected result
{
message: 'new record is saved',
data: {
level: 1,
multiplier:8
}
}