HenryAlexius
HenryAlexius

Reputation: 13

Error using req.params in an express put route

I would like to send two values as params using my Ajax put call. taskID is a integer and taskCompleted is a boolean. When taskID is two digits long (e.g. 12) my req.params shows:

{ id: 1, completed: 2false }

Below is the Ajax call and the put route I have. I want to understand how to send the parameters so that my taskID and taskCompleted do not separate.

 $.ajax({
        method: 'PUT',
        url: '/list/' + taskID + taskCompleted
router.put( '/:id:complete', ( req, res ) =>{
    console.log( 'in router.put:', req.params );
    let queryString;
    if ( req.params.complete == 'true' ){
        queryString = `UPDATE "tasks" SET "completed" = false WHERE "id" = $1`;
    }else {
        queryString = `UPDATE "tasks" SET "completed" = true WHERE "id" = $1`;
    }
    pool.query( queryString , [req.params.id]).then( (results) =>{
        res.sendStatus( 200 );
    }).catch( (err) =>{
        res.sendStatus( 500 );
        console.log( err );
    })
})//end router.put

Upvotes: 0

Views: 378

Answers (1)

Ali Nauman
Ali Nauman

Reputation: 1677

You need to separate the two values with a slash in your definition for the route i.e. make it router.put('/:id/:complete') in order for this to work.

I would be better if you send the completed parameter as part of the request body, as the body serves this purpose, while the url identifies the resource you want to update.

Upvotes: 1

Related Questions