I want to publish query result to handlebars view
Hi
I'm a junior DW engineer trying to make a simple nodeJS app with oracledb, so please forgive my ignorance about web development.
I am baffled by what seemed to be trivial task. I want to bind the query result of .execute() that all examples print out, outside the getConnection scope, and in turn publish the result onto the view.
I guess my question is, I do not know how to return the query result, so I can pass it from my nodeJS model to the router as a handlebars param.
Thanks in advance for taking out your time to answer this question. Below is the model.
module.exports = {
populateData : function () {
//I know this is wrong, but essentially I want to bind the query result that gets printed out to a queryResult var.
var queryResult = oracledb.getConnection(
{
user : "xxx",
password : "yyy",
connectString : "zzz"
},
function(err, connection){
if (err) {
console.error(err.message);
return;
}
return connection.execute(
"some Query",
function(err, result){
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
console.log(result.metaData);
console.log(result.rows);
doRelease(connection);
}
);
}
);
function doRelease(connection)
{
connection.release(
function(err) {
if (err) {
console.error(err.message);
}
});
}
return {
title: 'Alert',
data: queryResult
}
}
}
@sunghoonyang You're trying to export a function, populateData, that can return the data, but because Node.js is asynchronous, it will not work as you have it.
What is consuming the function, your code or someone else's?
The typical way to do this would be to have your function accept a callback function. Once you have the data you would invoke the callback function and pass it along (or an error if you encountered one).
Another option is to immediately return a Promise, which you would later resolve or reject. Do you know if the consumer of the function can work with callbacks or promises?
I know this is "way out of left field", but have you seen Oracle APEX? I started my dev career as a database developer and learned web development with APEX. It's an amazing tool because it's declarative (which is great for new people) but incredibly flexible. As a no-cost option that's included with your database license, it might be worth a look: http://apex.oracle.com
Thanks for the headsup - I got it working after reading up on async & callback. Thanks for your comment. I guess when you are ignorant, you don't even know where problem begins. def will check out APEX