What is the correct response to prevent "undefined" in custom endpoints?

default discord avatar
Harambe
7 months ago
4

If I overwrite the default create endpoint using custom endpoints within a collection, and I get the newly created object ID {insertedId: id}, which return statement can help me prevent /api/collection/undefined? res.status(200).send("what should be here?")



What is the correct response to prevent "undefined" in custom endpoints?

  • default discord avatar
    danielkraemer
    7 months ago

    Good morning, I currently don’t know your direct use case. If you need it directly, you should return the newly created id.



    Otherwise you should return HTTP Status code 201 created

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201
  • default discord avatar
    Harambe
    7 months ago

    Thanks alot, the 201 status code helped and what I was looking for was const response = {message: "my message", doc: payload} and the payload includes the id and other document properties.

  • default discord avatar
    danielkraemer
    7 months ago

    👍

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.