0

I want to pass as array of id's and with this array i want to delete multiple records from table in node js

Below code which i was trying but it's not working properly;

let deletedImageIds = [24081,222];

db.query(`delete from mx_product_images where productImageID in ?`, deletedImageIds, (err, res) => {
                  if (err) {
                    console.log("error: ", err);
                    callback(err, null); 
                    return;
                  }
                  callback(null, res);
                  return;
                })

0

1 Answer 1

1

This is because MySQL can't understand javascript array/objects. so please convert this javascript array to a comma-separated string, then append to the original query.

let deletedImageIds = [24081,222];
let del_str = deletedImageIds.join();

db.query(`delete from mx_product_images where productImageID in (?)`, del_str, (err, res) => {
                  if (err) {
                    console.log("error: ", err);
                    callback(err, null); 
                    return;
                  }
                  callback(null, res);
                  return;
                })
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.