Express: converts params to number

validatorHandler: it’s a middleware that validates schemas (check if the param :id is number, if not returns Error)
But Express converts all params into strings. SO, how can I turn :id into number BEFORE the middleware (validatorHandler) reads :id

router.delete('/:id',
  validatorHandler(getAuthorSchema),
  // Middleare
  async (req, res) => {
    const { id } = req.params
    try {
      const author = await service.deleteAuthor(+id)
      res.status(201).json({ data: author })
    } catch (error) {
      console.error(error)
    }
  }
)

I found the solution It’s pretty clear. Transform the params if its ID into number.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.