Make sure SQL DELETE Query executed in NodeJs

I am using the Delete Query in the NodeJs project, but it not returning the row length, OR I am not able to identify whether the query has been executed or not."/delete", async (req, res) => {
	try {
		let stmt = await otherDB.query("DELETE FROM `table` WHERE `column` = :value", { replacements: { value: "value" }, type: otherDB.QueryTypes.DELETE });
		if (stmt) { // if (stmt.length > 0  || stmt.affectedRows > 0) {
			return res.json({ code: 200, status: "success", message: "deleted" });
		} else {
			return res.json({ code: 200, status: "error", message: "not deleted" });
	} catch (err) {
		return res.json({ code: 500, message: { msg: "Query Error" }, status: "error", error: err.stack });

but it not working , please help How I achieve the goal.

Have you tried logging out stmt to see if there is useful property available to you? e.g.

let stmt = await otherDB.query("DELETE FROM `table` WHERE `column` = :value", { replacements: { value: "value" }, type: otherDB.QueryTypes.DELETE });
console.log('Statement:', JSON.stringify(stmt, null, 2))

It’s kind of new for me too. We are using npm mysql2. If I log out the response for deleting an item I get the following.

    "fieldCount": 0,
    "affectedRows": 1, ←
    "insertId": 0,
    "info": "",
    "serverStatus": 2,
    "warningStatus": 0

stmt.affectedRows > 0 I also use this but not working

What I am saying is, like I have done, log out stmt first to see what the response is.

I am getting this message in node terminal Statement: undefined

I’m sorry @wawane7256, this is sequelize isn’t it?

As I say still learning this myself, and have only really used Magic methods in sequelize. I have to say I think I might have preferred using raw queries.

The one thing that does standout though is

"DELETE FROM `table` WHERE `column` = :value", { replacements: { value: "value" }

Shouldn’t table be an actual table name, column be a column name? Also shouldn’t value be an actual value rather than “value”? — maybe one passed in on req

I guess it would be helpful to see an example of your table.

A bit out of my depth here, maybe someone else with more experience can better advise.

I am using this replacement method

@wawane7256 Yes, I know :slight_smile:

I don’t think there is an issue with your syntax. I think it’s more likely an issue with your query and the replacement value.

I am hazarding a guess `table` should be your actual table name, `column` should be an actual column name in the table, and “value” should be a matching value. e.g.
`table` could be `users`, column could be `id`and value could be 5 (

As I say an example of the table you are using would help.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.