javascript 如何在Nodejs中使用paramerized query

mfpqipee  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(91)

我正在使用Nodejs和expressjs,现在我正在尝试执行select query,query正在成功工作,但我想要两件事

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

下面是我的当前代码

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  
            }
    }
anauzrmj

anauzrmj1#

您可以将参数作为数组传递给execute调用,然后将行记录到控制台:

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)

输出结果如下所示:

Rows: [ { id: 1 } ]
svdrlsy4

svdrlsy42#

要打印包含在results中的值,只需使用results。

console.log(rows)

相关问题