我将 BatchError 与 executeMany 一起使用
如果没有错误..一切正常
但如果一个或多个错误.. 它不会执行。它给出 rowsAffected = 错误行号
我的代码:
const oracledb = require('oracledb');
async function post(req, res, next) {
try {
let user = {};
user = await create();
res.status(201).json(user);
} catch (err) {
next(err);
}
}
module.exports.post = post;
const sql =
`INSERT INTO TABLE (
USER_CODE,
USER_NAME,
STAFF_ID,
ROLE_ID,
TEAM_CODE,
GRP_ID,
MOBILE_NUMBER
) VALUES (
:USER_CODE,
:USER_NAME,
:STAFF_ID,
:ROLE_ID,
:TEAM_CODE,
:GRP_ID,
:MOBILE_NUMBER
)`;
async function create() {
const data = [
{"USER_CODE":600, "user_name": "att1", "staff_id": 660, "role_id": 1, "team_code": 20, "grp_id": 1, "mobile_number": "0101"},
{"USER_CODE":600, "user_name": "att2", "staff_id": 661, "role_id": 1, "team_code": 20, "grp_id": 1, "mobile_number": "0102"},
{"USER_CODE":602, "user_name": "att3", "staff_id": 662, "role_id": 1, "team_code": 20, "grp_id": 1, "mobile_number": "0103"}
];
let opts = {};
let result = await manyExecute(sql, data, opts);
return result;
}
module.exports.create = create;
function manyExecute(statement, binds, opts) {
return new Promise(async (resolve, reject) => {
let conn;
opts.outFormat = oracledb.OBJECT;
opts.autoCommit = true;
opts.batchErrors = true;
console.log(opts);
try {
conn = await oracledb.getConnection();
result = await conn.executeMany(statement, binds, opts);
resolve(result);
} catch (err) {
reject(err);
} finally {
if (conn) { // conn assignment worked, need to close
try {
await conn.close();
} catch (err) {
console.log(err);
}
}
}
});
}
module.exports.manyExecute = manyExecute;
如果我执行上面的代码,就会出现这个错误(USER_CODE 是 pk):
{ "rowsAffected": 2, "batchErrors": [ { "errorNum": 1, "offset": 1 } ] }
我也期望 rowsAffected = 3,第一项未添加到 TABLE
谢谢