I am new to node.js. I have created the below query and although it is not returning any errors I am not seeing the result (i.e data) of the SQL.
var oracledb = require('oracledb');
oracledb.getConnection(
{
user: "my_user",
password: "my_pwd",
connectString: "database_1"
},
function(err, connection){
if (err) {
console.error(err.message);
return;
}
connection.execute(
'select * from merchant_accounts'
),
{
maxRows: 3
},
function(err,result)
{
if (err) {
console.error(err.message);
//doRelease(connection);
return;
}
console.log(result);
//doRelease(connection);
}
})
CodePudding user response:
There is an extra close parenthesis )
after the string 'select * from merchant_accounts'
.
connection.execute(
'select * from merchant_accounts'
),
// ^------- oopsie-daisy!
By strange chance, what remains happens to be valid JavaScript syntax. The {
}
of your object literal becomes a code block. The maxRows:
becomes a label. and the 3
becomes an expression. It's valid to separate expressions with commas, and the function that follows is legally defined but discarded as part of the comma-separated expression.
Unfortunate happenstance, I suppose, for the reason that normally you'd get a syntax error by adding an extra parenthesis like that -- but in this case, by fluke, the syntax is valid so you didn't get a syntax error.
You are, of course, missing a close parenthesis too. The close parenthesis actually belongs here:
console.log(result);
//doRelease(connection);
})
// ^------ ah! much better
CodePudding user response:
Save yourself some headaches and follow all the current node-oracledb examples and use the async/await style instead of the older callback style you show.
Start with examples.js. Something like:
'use strict';
process.env.ORA_SDTZ = 'UTC';
const oracledb = require('oracledb');
const dbConfig = { user: 'cj', password: process.env.NODE_ORACLEDB_PASSWORD, connectString: 'localhost/orclpdb1' };
let libPath;
if (process.platform === 'win32') { // Windows
libPath = 'C:\\oracle\\instantclient_19_14';
} else if (process.platform === 'darwin') { // macOS
libPath = process.env.HOME '/Downloads/instantclient_19_8';
}
if (libPath && fs.existsSync(libPath)) {
oracledb.initOracleClient({ libDir: libPath });
}
let sql, binds, options, result;
sql = `select * from merchant_accounts`;
binds = [];
options = { outFormat: oracledb.OUT_FORMAT_OBJECT };
async function run() {
let connection;
try {
connection = await oracledb.getConnection(dbConfig);
result = await connection.execute(sql, binds, options);
console.dir(result, { depth: null });
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
run();
Also don't use maxRows
to limit the number of records shown, read the doc Limiting Rows and Creating Paged Datasets.