Error: Incorrect arguments to mysqld_stmt_execute in Nodejs

Dung Do Tien May 04 2022 20

Hello, I have an assignment need to use NodeJs and connect to MySQL to get list articles. I install MySQL by command npm install mysql and I have some javascript code to help get data as below:

 var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

async getArticles(num, page) {
    const fromIndex = (page - 1) * num;
    const statement = `SELECT id,title,sapo,content,publishdate FROM tlbArticles ORDER BY publishdate DESC LIMIT ? ,? ;`;
    const result = await con.execute(statement, [fromIndex, num]);
    console.log(result[0]);
    return result;
}

 But when running Node server with the command npm start I get an exception throw Error: Incorrect arguments to mysqld_stmt_execute.

 Error: Incorrect arguments to mysqld_stmt_execute
 at PromisePool.execute(C:\Projects\nodejs\learn_by_example\demo1\node_modules\mysql2\promise.js:359:22)
 at ArticleService.getArticles(C:\Projects\nodejs\learn_by_example\demo1\src\services\articleService.js:359:22)

I think I connected to DB but when executed select query, it threw this exception.

How can I fix it?

Thanks in advance.

Have 1 answer(s) found.
  • D

    Dheeraj Yadav May 04 2022

    I got the same error. I think execute() method with the second param, it only accepts string values so you need to convert fromIndexnum to string first.

    Solution: your variable can add more '' after them. See the example below:

     async getArticles(num, page) {
        const fromIndex = (page - 1) * num;
        const statement = `SELECT id,title,sapo,content,publishdate FROM tlbArticles ORDER BY publishdate DESC LIMIT ? ,? ;`;
        const result = await con.execute(statement, [fromIndex + '', num + '']);
        console.log(result[0]);
        return result;
    }

    It worked for me. 

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close