Node.js - CRUD API multi delete ID's from SQL

Viewed 38

I have small project with ReactJS + NodeJS + mySQL.

I can't create-receive correct request for multi delete by IDs.

This how I sent request from React to Node ==>

      const deleteProductsByIds = () => {
    let ids = [];
    stateProducts.forEach((d) => {
      if (d.select) {
        ids.push(d.id);
      }
    });
    axios
      .delete(`http://localhost:5000/products/${ids}`)
      .then((data) => {
        console.log(data);
        getProducts();
      })
      .catch((err) => alert(err));
  };

this how I receive request in Node(sequelize)

router.delete('/:ids', deleteProducts);



export const deleteProducts = async (req, res) => {
try {
    await Product.destroy({
        where: {
           id: []
        }
    });
    res.json({
        "message": "ProductS Deleted"
    });

} catch (error) {
    res.json({ message: error.message });
}  

in logs I have this data and by message everything fine, but products(301,302,303,304) not deleted.

config: {url: 'http://localhost:5000/products/301,302,303,304', 
method: 'delete', headers: {…}, 
transformRequest: Array(1), transformResponse: Array(1), …}
data: {message: 'ProductS Deleted'}

I try

 where: {
               id: req.params.ids
            }

but ids value undefind

also I try:

const ids = req.params
    try {
        await Product.destroy({
            where: {
               id: ids
            }
        });



message: "Invalid value { ids: '301,302,303,304' }"}

but receive error message.

usual delete request by ID working without any problem.

For example:

export const deleteProduct = async (req, res) => {
    try {
        await Product.destroy({
            where: {
                id: req.params.id
            }
        });
        res.json({
            "message": "Product Deleted"
        });
    } catch (error) {
        res.json({ message: error.message });
    }  
}

Please help me, because I can't find so much information about multi delete request with React-Node-mySQL.

0 Answers
Related