Supabase & ExpressJS having issues with errors

Viewed 33

I have been playing around with ExpressJS I normally use FastAPI. I can't seem to generate an error using Supabase.

I have this endpoint

app.delete('/api/delete-book/:id', cors(corsOptions), async (req, res) => {
  const {data, error} = await supabase
    .from('books-express')
    .delete()
    .match({id: req.params.id})

  if (error) {
    res.status(400).send({message: `ERROR! ${error.message}`})
  }

  if (data)
    res.send({
      message: `Book ID ${req.params.id} has been deleted from the database`,
    })
})

This works when it comes to deleting a book via an ID. However if I enter an invalid ID I get the data if block firing.

There is no book with an ID of 222 in the database, I would expect the error to fire but its just null

Any ideas here?

1 Answers

This is expected behaviour; not matching any rows is not considered an error condition in postgres.

If you'd like to check if any rows were deleted, you can use something akin to (on supabase-js 2.x):

const { data, error } = await supabase.from('books-express')
  .delete()
  .match({id: req.params.id})
  .select() // not needed on 1.x libs

if (error || data.length === 0) {
  res.status(400).send({...})
}
Related