我正在用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);
}
}
}
};
这是我尝试插入单个记录的控制器方法,在生产环境中,绑定数据将来自post请求。
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语句不是问题,简单插入也会崩溃。
1条答案
按热度按时间6ie5vjzr1#
尝试将参数值作为字符串而不是数字发送,这解决了我的问题。
参考:www.example.comhttps://github.com/oracle/node-oracledb/issues/1377#issuecomment-1346171847