Node.JS 仅返回 MySQL "WHERE IN" 子句的部分记录

Node.JS is returning only a part of records for the MySQL "WHERE IN" clause

我正在尝试执行以下 GET 函数。

http://127.0.0.1:3000/sportfolioimages/getbyportfoliolist?portfolioid_list=69,70,71

我的代码在Node.js中,查看下方。

const mysql = require('mysql2');
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');

const prop = PropertiesReader('properties.properties');

const con = mysql.createConnection({
  host: prop.get('server.host'),
  user: prop.get("server.username"),
  password: prop.get("server.password"),
  port: prop.get("server.port"),
  database: prop.get("server.dbname")
});


exports.getSellerPortfolioItemImagesByPortfolioList = (event, context, callback) => {

  const params = event.queryStringParameters;


  if (!params || portfolioid_list == null) {
    context.callbackWaitsForEmptyEventLoop = false;
    var response = errorCodes.missing_parameters;
    callback(null, response)
  }
  else {

    const portfolioid_list = event.queryStringParameters.portfolioid_list;
    context.callbackWaitsForEmptyEventLoop = false;

    const sql = "SELECT * FROM peresia.seller_portfolio_item_images WHERE idseller_portfolio_item IN (?)";
      con.execute(sql, [portfolioid_list], function (err, result) {
      console.log(sql);
        if (err) {
          console.log(err);
          var response = errorCodes.internal_server_error;
          callback(null, response);
        }
        else {
          var response = {
            "statusCode": 200,
            "headers": {
              "Content-Type": "application/json"
            },
            "body": JSON.stringify(result),
            "isBase64Encoded": false
          };
          callback(null, response)
        }
      });


  }
};

我的代码总是 returns 无论我调用中值列表第一个的值是什么。由于我的值列表是 69,70,71 它总是 returns 只有匹配 69 的记录并且没有返回 7071 的记录,即使有数据库中的记录。

举个例子,下面是我执行上述GET函数时得到的结果,

[
    {
        "idseller_portfolio_item_images": 22,
        "idseller_portfolio_item": 69,
        "image_url": "https://database.com/portfolio/IMG_20211020_114254-1634730049335.jpg"
    },
    {
        "idseller_portfolio_item_images": 23,
        "idseller_portfolio_item": 69,
        "image_url": "https://database.com/portfolio/IMG_20211020_114254-1634730049335.jpg"
    },
    {
        "idseller_portfolio_item_images": 31,
        "idseller_portfolio_item": 69,
        "image_url": "https://peresia3.s3.us-east-2.amazonaws.com/portfolio/IMG_20211020_114254-1634730049335.jpg"
    },
    {
        "idseller_portfolio_item_images": 32,
        "idseller_portfolio_item": 69,
        "image_url": "https://database/portfolio/IMG_20211020_114254-1634730049335.jpg"
    }
]

如果我直接在数据库中 运行 MySQL 代码,我将能够毫无问题地获取完整的记录集。

为什么会这样,我该如何解决?

根据 this 你需要这样的东西

const portfolioid_list = [69,70,71];

const sql = "SELECT * FROM peresia.seller_portfolio_item_images WHERE idseller_portfolio_item IN (?,?,?)";
      con.execute(sql, portfolioid_list, function (err, result) { ...

这样您就可以动态构建查询。 reference

const portfolioid_list = event.queryStringParameters.portfolioid_list.split(",");  //converts to a list ["69", "70", "71"]
    
portfolioidValsPlaceHolders=Array(portfolioid_list.length).fill("?").join();
const sql = "SELECT * FROM peresia.seller_portfolio_item_images WHERE idseller_portfolio_item IN ("+portfolioidValsPlaceHolders+")"; 
      con.execute(sql, portfolioid_list, function (err, result) { ...