无法在 Mysql 和 Express Node JS 中转义限制值

Can't Escape Limit Value in Mysql & Express Node JS

我正在用 Node.js (+Express JS) 和 MySQL 开发一个 API。我无法在查询中转义或添加自定义限制值。这是我的路线;

app.post('/api/v1/fetch/limitedArticles/:limit', (req, res,) => {

    const limitValue = req.params.limit;

    let query = "SELECT article_id, article_name, article_image, article_url FROM ?? ORDER BY article_id DESC LIMIT ?";

    mysqlDB.query(query, ['app_articles', limitValue], function (mysqlError, mysqlRes) {
        if(mysqlError){
            return res.status(500).send({message: "error_server_db"});
        }else if(mysqlRes.length<1){
            return res.status(404).send({message: "warning_empty_list"});
        }else{
            //All good
            return res.status(200).send(mysqlRes);
        }
        throw mysqlError;

    })
});

我请求为;

{{api_base_url}}/api/v1/fetch/limitedArticles/2

我收到这个错误;

{
"message": {
    "code": "ER_PARSE_ERROR",
    "errno": 1064,
    "sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''2'' at line 1",
    "sqlState": "42000",
    "index": 0,
    "sql": "SELECT article_id, article_name, article_image, article_url FROM `app_articles` ORDER BY article_id DESC LIMIT '2'"
}

}

如果我删除参数值并键入“2”作为我的限制值(例如 LIMIT 2),它会起作用。但是它并没有像我展示的那样工作。

limitValue 是一个字符串 - 您应该将其转换为数字,以便可以在 limit 子句中使用:

const limitValue = Number(req.params.limit);