Search code examples
node.jsexpressmern

Product.findByIdAndRemove is not a function Node.js


    router.delete("/:productId", async (req, res) => {
    try {
        const productId = req.params.productId;

        const deletedProduct = await Product.findByIdAndRemove(productId);

        if (!deletedProduct) {
            return res.status(404).json({ error: "Product not found." });
        }

        res.status(200).json(deletedProduct);
    } catch (error) {
        console.log(error);
        res.status(500).json({ error: "Server error." });
    }
});

It was working, after changing my post method works with multer, it stopped working and giving this error


Solution

  • findByIdAndRemove is deprecated in Mongoose version 8.0.0 and later. Instead, you should use findByIdAndDelete