skip to Main Content

I am working with Nodejs and expressjs,Right now i am trying to execute select query , query is working successfully but i want two things

1) How to convert this query to "paramerized query" ?
2) Whenever i am trying to get "total rows" then i am getting "[object object] "

Here is my current code

static async addServiceDetail(shopId,serviceId,genderTypeId,price,serviceName)
    {
        const sql = `SELECT id from hc_servicesdetail WHERE shopId='${shopId}' AND  serviceId='${serviceId}' AND genderTypeId='${genderTypeId}'`;
        const [rows, fields] = await pool.execute(sql);
         console.log('total rows are'+ rows); // getting [object object]
        if(rows<1)
            {
                  //further code  
            }
    }

2

Answers


  1. For printing the value that is contained inside results just use results.

    console.log(rows)
    
    Login or Signup to reply.
  2. You can pass parameters as an array to the execute call, then log the rows to the console:

    let shopId = 'B';
    let serviceId = 'k';
    let genderTypeId = 'F';
    let sql = 'SELECT id from hc_servicesdetail WHERE shopId=? AND serviceId=? AND genderTypeId=?';
    const [rows, fields] = await pool.execute(sql, [shopId, serviceId, genderTypeId]);
    console.log('Rows:', rows)
    

    The output will look something like this:

    Rows: [ { id: 1 } ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search