1

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
10
  • I think you'll have to show us the code for playlist.save() and for the Playlist() constructor because there's nothing obvious in the code you have shown that would cause that error. Commented Jun 3, 2020 at 23:44
  • It's hard to know without seing more of your code, but either new Playlist() is a Promise under the hood, or playlist.save() is doing something funky. I'd suggest adding console logs and seeing which one of those two things is actually failing Commented Jun 3, 2020 at 23:45
  • .save is a mongoose method @jfriend00 Commented Jun 3, 2020 at 23:45
  • Yeah, but isn't there some model code somewhere of yours involved in the .save() process? Because the code you've shown here will not cause UnhandledPromiseRejectionWarning by itself. So, something ELSE must be contributing. It's also possible that new Playlist() is throwing and that gets reported as an UnhandledPromiseRejectionWarning because it's in an async function. Commented Jun 3, 2020 at 23:46
  • 1
    The error doesn't give you any more specifics, like the error message and which line number the error is on? Commented Jun 3, 2020 at 23:47

2 Answers 2

1

I'd recommend you also wrap that first part in a try/catch. If req.body somehow doesn't get populated, or if new Playlist throws any sort of error, since this is an async function, that'll become a rejected Promise. This is safer:

router.post('/', async (req, res) => {
    try {
        const playlist = new Playlist({
            song: req.body.song,
            artist: req.body.artist
        })
        const newPlaylist = await playlist.save()
        res.status(201).json(newPlaylist)
    } catch (err) {
        res.status(400).json({ message: err.message })
    }
})

If you're getting a "Cannot read property 'song' of undefined" error, that means that the request body could not be parsed and remains undefined. Maybe the wrong content-type header was sent or you don't have a body parsing middleware set up correctly.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks I dont get the error anymore which is great. But when I make a post request using Postman, I get this error { "message": "Cannot read property 'song' of undefined" }. So maybe I probably didnt set up the body parsing properly
Is this supposed to process JSON? If so, is postman sending content-type: application/json? Is express configured to parse JSON?
If you haven't set up your body parsing middleware, see github.com/expressjs/body-parser
I read somewhere that express now comes with the body parser, so I didnt install the body parser separately. I will try that now.
0

You have handled the exception using try ... catch and this is great. Although outside of this try catch can be an issue. So there might to be two errors here

  1. Either req.body.song or req.body.artist OR Playlist is not a valid class
  2. On your catch block res.status(400).json({ message: err.message }) this could be another issue.

It would be great if you try and catch entire code block and log the err and it would be clear.

UnhandledPromiseRejectionWarning is happened because you didn't catch the exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.