【问题标题】:Node-orcaledb: execute() and executeMany() crash without errorNode-orcaledb:execute() 和 executeMany() 崩溃而没有错误
【发布时间】:2023-02-17 02:13:17
【问题描述】:

我正在使用 node-oracledb 构建一个 api rest,我可以检索所有类型的数据,但每次我尝试插入时程序都会中断。 这是我用来从池中获取连接并执行查询的通用方法。

import oracledb from "oracledb";

export const executeQuery = async ({ query, binds, options, type, res }) => {
  let connection = null;

  try {
    connection = await oracledb.getConnection();
  } catch (error) {
    console.log("Error al conectar OracleDB");
  }
  let result = null;
  try {
    result =
      type === "insertOne"
        ? await connection.execute(query, binds, options)
        : type === "insertMany"
        ? await connection.executeMany(query, binds, options)
        : null;
    console.log(result);
  } catch (err) {
    console.error("error", err.message);
    res.status(500).json("Error recuperando datos");
  } finally {
    if (connection) {
      try {
        res.status(200).json(result.rows);
        // Always release the connection back to the pool
        await connection.close();
      } catch (err) {
        return console.error(err.message);
      }
    }
  }
};

这是我尝试插入单个记录的控制器方法,在生产中绑定数据将来自发布请求。

insertOneExample: async (req, res) => {
    const { items } = req.body;

    const query = `MERGE INTO SCHEMA.TABLE USING dual ON (CODIGO_HOSPI = :CODIGO_HOSPI AND CENTRO_ID = :CENTRO_ID) 
    WHEN MATCHED THEN UPDATE SET PREV1 = :PREV1, PREV2 = :PREV2, PREV3 = :PREV3, PREV4 = :PREV4, PREV5 = :PREV5, PREV6 = :PREV6, PREV7 = :PREV7, PREV8 = :PREV8, PREV9 = :PREV9, PREV10 = :PREV10, PREV11 = :PREV11, PREV12 = :PREV12,
    WHEN NOT MATCHED THEN INSERT (CODIGO_HOSPI, PREV1, PREV2, PREV3, PREV4, PREV5, PREV6, PREV7, PREV8, PREV9, PREV10, PREV11, PREV12, CENTRO_ID)
    VALUES (:CODIGO_HOSPI, :PREV1, :PREV2, :PREV3, :PREV4, :PREV5, :PREV6, :PREV7, :PREV8, :PREV9, :PREV10, :PREV11, :PREV12, :CENTRO_ID)`

    const options = {
      autoCommit: true,
      bindDefs: {
        CODIGO_HOSPI: { type: oracledb.STRING, maxSize: 20 },
        PREV1: { type: oracledb.NUMBER },
        PREV2: { type: oracledb.NUMBER },
        PREV3: { type: oracledb.NUMBER },
        PREV4: { type: oracledb.NUMBER },
        PREV5: { type: oracledb.NUMBER },
        PREV6: { type: oracledb.NUMBER },
        PREV7: { type: oracledb.NUMBER },
        PREV8: { type: oracledb.NUMBER },
        PREV9: { type: oracledb.NUMBER },
        PREV10: { type: oracledb.NUMBER },
        PREV11: { type: oracledb.NUMBER },
        PREV12: { type: oracledb.NUMBER },
        CENTRO_ID: { type: oracledb.STRING, maxSize: 10 },
      },
    };;
  
    executeQuery({
      query,
      binds: {
        CODIGO_HOSPI: "101",
        PREV1: 52600,
        PREV2: 870,
        PREV3: 123,
        PREV4: 564,
        PREV5: 846,
        PREV6: 625,
        PREV7: 897,
        PREV8: 124,
        PREV9: 656,
        PREV10: 456,
        PREV11: 324,
        PREV12: 212,
        CENTRO_ID: "10346",
      },
      options,
      type: "insertOne",
      res,
    });
  }

执行该方法时,服务器崩溃而没有任何错误消息。

no error crash

*** sql 语句不是问题,简单的插入也会崩溃。

【问题讨论】:

  • 我从快速扫描中看到的唯一事情是当result 为 null 时,您可以引用 result.rows,并且在没有连接的情况下不使用 res。尝试通过删除列和绑定值(甚至所有这些来检查基本流程)来简化您的代码。您可能认为该语句不是问题,但您需要对其进行验证。不要链接.json,而是将返回值放入一个变量中并检查它是否正常。如果您仍然需要更多帮助,请使用可运行的脚本更新问题并包含 CREATE TABLE。见stackoverflow.com/help/minimal-reproducible-example

标签: node.js oracle node-oracledb


【解决方案1】:

尝试将参数值作为字符串而不是数字发送,这解决了我的问题。

参考:https://github.com/oracle/node-oracledb/issues/1377#issuecomment-1346171847

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多