node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Not receiving response after deleting row

Open SyedImam1998 opened this issue 3 years ago • 5 comments
trafficstars

Hi Team, I am using mysql2 package with expressjs this is my code

app.post('/deleteNotPaid',(req, res)=>{
    const sno=parseInt(req.body.sno);
    db.query("DELETE FROM notpaid WHERE Sno=?",sno,(err,result)=>{
      if(err){
    console.log(err)
}else{
   res.send("Deleted")
}
     
    });

here when i tried to call the the api, data is getting deleted in DB but i am not getting res.send message. when i tried in postman it just calling not getting response. Please help me here what can i do to get response

SyedImam1998 avatar Aug 01 '22 12:08 SyedImam1998

are you using the "classic" api or promise one? Can you show us the code that creates db?

sidorares avatar Aug 02 '22 23:08 sidorares

const db=mysql.createConnection({ 
  user:"root", 
  host:"localhost", 
  password:"syedimam", 
  database:"data" 
})

@sidorares Here it is...

SyedImam1998 avatar Aug 06 '22 13:08 SyedImam1998

Is mysql result of require('mysql2') or require('mysql2/promise')?

sidorares avatar Aug 07 '22 00:08 sidorares

It's just require('mysql2'). On Sun, 7 Aug 2022, 6:12 am Andrey Sidorov, @.***> wrote:

Is mysql result of require('mysql2') or require('mysql2/promise')?

— Reply to this email directly, view it on GitHub https://github.com/sidorares/node-mysql2/issues/1602#issuecomment-1207305498, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMNYQDMFWBRRZLDLGJMYYULVX4BAVANCNFSM55HOZIMA . You are receiving this because you authored the thread.Message ID: @.***>

SyedImam1998 avatar Aug 07 '22 03:08 SyedImam1998

if you reduce that to just a simple script ( outside of express app ) would you still be able to reproduce the issue (e.i below script not printing anything at all)?

const mysql = require('mysql2');
const db=mysql.createConnection({ 
  user:"root", 
  host:"localhost", 
  password:"syedimam", 
  database:"data" 
})
const sno = 123;
db.query("DELETE FROM notpaid WHERE Sno=?",sno, (err, result) => {
  if (err) {
     console.log('Error:', err);
  } else {
    console.log('Deleted', result);
  }
  db.close();
});

sidorares avatar Aug 07 '22 03:08 sidorares

I am not getting the response saying "2 rows effected" response once the rows are deleted

On Sun, 7 Aug 2022, 9:21 am Andrey Sidorov, @.***> wrote:

if you reduce that to just a simple script ( outside of express app ) would you still be able to reproduce the issue (e.i below script not printing anything at all)?

const mysql = require('mysql2');const db=mysql.createConnection({ user:"root", host:"localhost", password:"syedimam", database:"data" })const sno = 123;db.query("DELETE FROM notpaid WHERE Sno=?",sno, (err, result) => { if (err) { console.log('Error:', err); } else { console.log('Deleted', result); } db.close();});

— Reply to this email directly, view it on GitHub https://github.com/sidorares/node-mysql2/issues/1602#issuecomment-1207325443, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMNYQDM6KHOBVH7X7GIUJ5TVX4XEVANCNFSM55HOZIMA . You are receiving this because you authored the thread.Message ID: @.***>

SyedImam1998 avatar Oct 11 '22 08:10 SyedImam1998

Depends on how db.query() is called.

results = await db.query("DELETE FROM ...", blabla,(err) => {}) gives you what you want. and await db.query("DELETE FROM ...", blabla,(err, result) => {}) gives you nothing.

Both deletes data if matched.

import { createConnection } from 'mysql2/promise';
import dotenv from 'dotenv'

dotenv.config()
const { DB_HOST, DB_USER, DB_PASS, DB_NAME } = process.env

const db = await createConnection({
  host: DB_HOST,
  user: DB_USER,
  password: DB_PASS,
  database: DB_NAME
})

let results;
let fields;
let status = 1;

[results, fields] = await db.query('SELECT firstname, lastname, status FROM users WHERE status = ?', status)

console.log('Selected', results)
console.log('fields', fields.map(f => `${f.name}`))

results = await db.query("DELETE FROM users WHERE status=?", status, (err) => {
  if (err) {
    const error = {
      code: err.code,
      message: err.sqlMessage,
      sql: err.sql
    }
    console.error(error)
  }
})

console.log('Deleted #1:', results)

status = 4
await db.query("DELETE FROM users WHERE status=?", status, (err, result) => {
  if (err) {
    console.log('Error:', err);
  } else {
    console.log('Deleted #2', result);
  }
  db.close();
});

output

[nodemon] starting `node mysql/src/delete.js`
Selected [ { firstname: 'John', lastname: 'Doe', status: 1 } ]
fields [ 'firstname', 'lastname', 'status' ]
Deleted #1: [
  ResultSetHeader {
    fieldCount: 0,
    affectedRows: 1,
    insertId: 0,
    info: '',
    serverStatus: 34,
    warningStatus: 0
  },
  undefined
]

trasherdk avatar Nov 28 '22 07:11 trasherdk