I have this function in node and express
router.post('/', async (req, res) => {
const playlist = new Playlist({
song: req.body.song,
artist: req.body.artist
})
try {
const newPlaylist = await playlist.save()
res.status(201).json(newPlaylist)
} catch (err) {
res.status(400).json({ message: err.message })
}
})
However, I am getting this error
(node:23242) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'song' of undefined
playlist.save()and for thePlaylist()constructor because there's nothing obvious in the code you have shown that would cause that error..save()process? Because the code you've shown here will not causeUnhandledPromiseRejectionWarningby itself. So, something ELSE must be contributing. It's also possible thatnew Playlist()is throwing and that gets reported as anUnhandledPromiseRejectionWarningbecause it's in anasyncfunction.