hisham droubi
hisham droubi

Reputation: 29

why is making routes in this way does not work

I was trying to make different methods for one route in a short and easy way that i saw in a video put i got this error Error: Route.post() requires a callback function but got a [object String] i will put the part that made this problem below

usersRouter.route('/whishlist')
    .post('/add', (req, res) => {
        try {
            let appartment_id = req.body.appartment_id;
            let customer_id = 405521014;
            let sql = 'insert into wishlist (customer_id,appartment_id)values(?,?)';
            connection.query(sql,[customer_id,appartment_id],(err,result)=>{
                if(err)
                throw err;
                res.send('added to wishlist successfully');
            })
        } catch (error) {
            
        }
    })
    .get('/show', (req, res) => {
        try {
            let customer_id = 405521014;
            let sql = 'select * from wishlist'
            connection.query(sql, (err, result) => {
                if (err)
                    throw err;
                res.send(JSON.stringify(result));
            })

        } catch (error) {         
        }
    })
    .delete('/remove', (req, res) => {
        try {
            let appartment_id = req.body.appartment_id;
            let customer_id = 405521014;
            let sql = 'delete from wishlist where customer_id=? and appartment_id=?';
            connection.query(sql,[customer_id,appartment_id],(err,result)=>{
                if(err)
                throw err;
                res.send('remover from wishlist successfully');
            })
        } catch (error) {
            
        }
    });

module.exports = usersRouter;

Upvotes: 0

Views: 30

Answers (1)

Kelvin Schoofs
Kelvin Schoofs

Reputation: 8718

I assume you're using Express

It seems similar to the example under app.route() here. According to the router documentation, you can't "sub route" that route that way.

Upvotes: 1

Related Questions