Node.js + Oracledb 4.2 + executeMany + Error: NJS-011: encountered bind value and type mismatch
Node.js + Oracledb 4.2 + executeMany + Error: NJS-011: encountered bind value and type mismatch
我正在使用 Node.js 12.16.1 和 npm oracle v4.2、Oracle 11g、Windows 10 机器并尝试使用 executeMany
命令插入批量数据,如下所示。
创建TABLE
CREATE TABLE DOWNTIME_HOURLY
(
SHIFTDAY NUMBER NOT NULL,
MACHINENAME VARCHAR2(20) NOT NULL,
PLANID VARCHAR2(20) NOT NULL,
PARTNAME VARCHAR2(20) NOT NULL,
CATEGORY VARCHAR2(20) NOT NULL,
SHIFTDATE DATE NOT NULL
);
NODE.js 代码
const oracledb = require('oracledb');
const credentials = { user: 'asdasdasd', password: 'asdasdasd!23', connectionString: 'hostname/ORCL' };
const options = {
autoCommit: true,
bindDefs: {
"SHIFTDAY": { type: oracledb.NUMBER },
"MACHINENAME": { type: oracledb.STRING, maxSize: 20 },
"PLANID": { type: oracledb.STRING, maxSize: 20 },
"PARTNAME": { type: oracledb.STRING, maxSize: 20 },
"CATEGORY": { type: oracledb.STRING, maxSize: 20 },
"SHIFTDATE": { type: oracledb.DATE }
}
};
const bindings = [
{
"SHIFTDAY": 12,
"MACHINENAME": "test",
"PLANID": "test",
"PARTNAME": "test",
"CATEGORY": "test",
"SHIFTDATE": "03-APR-20"
}];
const sql = `INSERT INTO DOWNTIME_HOURLY
(SHIFTDAY,MACHINENAME,PLANID,PARTNAME,CATEGORY,SHIFTDATE)
VALUES
(:SHIFTDAY, :MACHINENAME, :PLANID, :PARTNAME, :CATEGORY, :SHIFTDATE)`
const insert = async (credentials) => {
const conn = await oracledb.getConnection(credentials).catch(err => console.log('ERRRRR....', err));
console.log('Connection successful');
oracledb.fetchAsString = [oracledb.DATE];
let result = await conn.execute(`SELECT current_date, current_timestamp FROM DUAL`);
console.log(result);
result = await conn.executeMany(sql, bindings, options).catch(err => console.log('Execution ERRRRR....', err));
console.log('Query executed: ' , result);
}
insert(credentials);
输出:
PS D:\project> node try
Connection successful
{
metaData: [ { name: 'CURRENT_DATE' }, { name: 'CURRENT_TIMESTAMP' } ],
rows: [ [ '03-APR-20', '03-APR-20 10.43.09.643015 AM UTC' ] ]
}
Execution ERR.... Error: NJS-011: encountered bind value and type mismatch
at Connection.executeMany (D:\project\node_modules\oracledb\lib\connection.js:203:21)
at D:\project\node_modules\oracledb\lib\util.js:202:16
at new Promise (<anonymous>)
at Connection.executeMany (D:\project\node_modules\oracledb\lib\util.js:190:14)
at insert (D:\project\try.js:36:25)
Query executed: undefined
问题:
有人可以指导我解决上述错误。我怀疑这是为了 SHIFTDATE
。非常感谢您。
最有可能的问题是日期格式,需要匹配会话日期格式NLS_DATE_FORMAT。
根据文档,https://oracle.github.io/node-oracledb/doc/api.html#oracledbconstantsnodbtype
问题出在日期格式上。然后我必须使用 node-oracledb 4.2
.
中已经支持的 Javascript 日期(即 new Date()
)
我按以下方式传递了值并且它起作用了。
const bindings = [
{
"SHIFTDAY": 12,
"MACHINENAME": "test",
"PLANID": "test",
"PARTNAME": "test",
"CATEGORY": "test",
"SHIFTDATE": new Date()
}];
我正在使用 Node.js 12.16.1 和 npm oracle v4.2、Oracle 11g、Windows 10 机器并尝试使用 executeMany
命令插入批量数据,如下所示。
创建TABLE
CREATE TABLE DOWNTIME_HOURLY
(
SHIFTDAY NUMBER NOT NULL,
MACHINENAME VARCHAR2(20) NOT NULL,
PLANID VARCHAR2(20) NOT NULL,
PARTNAME VARCHAR2(20) NOT NULL,
CATEGORY VARCHAR2(20) NOT NULL,
SHIFTDATE DATE NOT NULL
);
NODE.js 代码
const oracledb = require('oracledb');
const credentials = { user: 'asdasdasd', password: 'asdasdasd!23', connectionString: 'hostname/ORCL' };
const options = {
autoCommit: true,
bindDefs: {
"SHIFTDAY": { type: oracledb.NUMBER },
"MACHINENAME": { type: oracledb.STRING, maxSize: 20 },
"PLANID": { type: oracledb.STRING, maxSize: 20 },
"PARTNAME": { type: oracledb.STRING, maxSize: 20 },
"CATEGORY": { type: oracledb.STRING, maxSize: 20 },
"SHIFTDATE": { type: oracledb.DATE }
}
};
const bindings = [
{
"SHIFTDAY": 12,
"MACHINENAME": "test",
"PLANID": "test",
"PARTNAME": "test",
"CATEGORY": "test",
"SHIFTDATE": "03-APR-20"
}];
const sql = `INSERT INTO DOWNTIME_HOURLY
(SHIFTDAY,MACHINENAME,PLANID,PARTNAME,CATEGORY,SHIFTDATE)
VALUES
(:SHIFTDAY, :MACHINENAME, :PLANID, :PARTNAME, :CATEGORY, :SHIFTDATE)`
const insert = async (credentials) => {
const conn = await oracledb.getConnection(credentials).catch(err => console.log('ERRRRR....', err));
console.log('Connection successful');
oracledb.fetchAsString = [oracledb.DATE];
let result = await conn.execute(`SELECT current_date, current_timestamp FROM DUAL`);
console.log(result);
result = await conn.executeMany(sql, bindings, options).catch(err => console.log('Execution ERRRRR....', err));
console.log('Query executed: ' , result);
}
insert(credentials);
输出:
PS D:\project> node try
Connection successful
{
metaData: [ { name: 'CURRENT_DATE' }, { name: 'CURRENT_TIMESTAMP' } ],
rows: [ [ '03-APR-20', '03-APR-20 10.43.09.643015 AM UTC' ] ]
}
Execution ERR.... Error: NJS-011: encountered bind value and type mismatch
at Connection.executeMany (D:\project\node_modules\oracledb\lib\connection.js:203:21)
at D:\project\node_modules\oracledb\lib\util.js:202:16
at new Promise (<anonymous>)
at Connection.executeMany (D:\project\node_modules\oracledb\lib\util.js:190:14)
at insert (D:\project\try.js:36:25)
Query executed: undefined
问题:
有人可以指导我解决上述错误。我怀疑这是为了 SHIFTDATE
。非常感谢您。
最有可能的问题是日期格式,需要匹配会话日期格式NLS_DATE_FORMAT。
根据文档,https://oracle.github.io/node-oracledb/doc/api.html#oracledbconstantsnodbtype
问题出在日期格式上。然后我必须使用 node-oracledb 4.2
.
new Date()
)
我按以下方式传递了值并且它起作用了。
const bindings = [
{
"SHIFTDAY": 12,
"MACHINENAME": "test",
"PLANID": "test",
"PARTNAME": "test",
"CATEGORY": "test",
"SHIFTDATE": new Date()
}];