Home > Enterprise >  Delete route, .remove not a function?
Delete route, .remove not a function?

Time:05-04

I´ve created a route to delete a product but when I try to run it, I get the error .remove is not a function. I don't know why this is occurring or how to fix it, I have looked only haven't found anything that works. My route looks like this:

orderRouter.delete(
    '/:id',
    isAuth,
    isAdmin,
    expressAsyncHandler(async (req, res) => {
        const order = await Orderline.findByPk(req.params.id);
        if (order) {
            const deletedOrder = await order.remove();
            res.send({ message: 'Order Deleted', product: deletedOrder });
        } else {
            res.status(404).send({ message: 'Order Not Found' });
        }
    })
);

Any guidance/help is much appreciated.

CodePudding user response:

In Sequelize ORM, Instance.remove() is not a function. Use Instance.destroy() instead.

Refer to the documentation about it here

So your fixed code would be:

orderRouter.delete(
    '/:id',
    isAuth,
    isAdmin,
    expressAsyncHandler(async (req, res) => {
        const order = await Orderline.findByPk(req.params.id);
        if (order) {
            const deletedOrder = await order.destroy();
            res.send({ message: 'Order Deleted', product: deletedOrder });
        } else {
            res.status(404).send({ message: 'Order Not Found' });
        }
    })
);
  • Related