Matt Berry
Matt Berry

Reputation: 109

"Invalid input syntax for integer " error on Express response.redirect

I'm getting the below error when I've run a PUT query into Postgres via Express.

error: invalid input syntax for integer: "all-calls"

It appears to be referring to this snippet of code in the router.put function response.redirect('all-calls'). The data is updating in the database fine.

This is the first POST request I've created. All other redirects have been POST and the response.redirect('all-calls') has worked perfectly fine on them.

Problematic PUT route:

router.put('/update-call-simple/:id', (request, response, next) => {
  console.log(request.body)
  const { id } = request.params;
  const { organisation_id, caller_name, call_contents, support_agent, priority, category, id_vendor } = request.body;
  pool.query('UPDATE calls SET id_organisation=($1), caller_name=($2), call_contents=($3), support_agent=($4), priority=($5), category=($6), id_vendor=($7) WHERE calls_id=($8)',
    [organisation_id, caller_name, call_contents, support_agent, priority, category, id_vendor, id],
    (err, res) => {
      if (err) return next(err);
      response.redirect('all-calls');
    })
})

Example of PUT route that works perfectly fine:

router.post('/new-call-simple', (request, response, next) => {
  const { organisation_id, caller_name, call_contents, support_agent, priority, category, id_vendor } = request.body;
  pool.query('INSERT INTO calls (id_organisation, caller_name, call_contents, support_agent, priority, category, id_vendor, date_time) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)',
    [organisation_id, caller_name, call_contents, support_agent, priority, category, id_vendor, Date.now()],
    (err, res) => {
      if (err) return next(err);
      response.redirect('all-calls');
    }
  )
})

Upvotes: 0

Views: 386

Answers (1)

1565986223
1565986223

Reputation: 6718

You're using relative path and express is unable to resolve all-calls path relative to /update-call-simple/:id. Unless you've a route /update-call-simple/:id/all-calls the redirect is not going to work. And express is probably assuming this is a status-code input.

Use the / to make your path relative to root.

For example in you've a route:

router.put('/path/to/somewhere' ...

And redirecting to this page in your code elsewhere:

...
res.redirect('/path/to/somewhere')

Upvotes: 1

Related Questions