Node.js multiple query transactions - javascript

I'm using the following function for handling multiple query transactions:
db.js
function waterfall (tasks, callback) {
pg.connect(conString, function (err, client, done) {
if (err) {
return callback(err);
}
//client.query(begin_transaction, function (err) {
client.query('BEGIN', function (err) {
if (err) {
done();
return callback(err);
}
var wrapIterator = function (iterator) {
return function (err) {
if (err) {
//client.query(rollback_transaction, function () {
client.query('ROLLBACK', function () {
done();
callback(err);
});
}
else {
var args = Array.prototype.slice.call(arguments, 1);
var next = iterator.next();
if (next) {
args.unshift(client);
args.push(wrapIterator(next));
}
else {
args.unshift(client);
args.push(function (err, results) {
var args = Array.prototype.slice.call(arguments, 0);
if (err) {
//client.query(rollback_transaction, function () {
client.query('ROLLBACK', function () {
done();
callback(err);
});
}
else {
//client.query(commit_transaction, function () {
client.query('COMMIT', function () {
done();
callback.apply(null, args);
})
}
})
}
async.setImmediate(function () {
iterator.apply(null, args);
});
}
};
};
wrapIterator(async.iterator(tasks))();
});
});
}
(referred from http://baudehlo.com/2014/04/28/node-js-multiple-query-transactions/)
What's wrong with the following function:
plot.js
db.waterfall([
function(client,cb){
client.query("INSERT INTO mydb.plotsold" +
"(plot_id, agent_id, plot_price, plot_date) VALUES " +
"($1,$2,$3,$4) RETURNING id", soldInfo.PlotId, soldInfo.agentId,
soldInfo.soldPrice, soldInfo.newDate,cb);
},
function(client,results,cb){
client.query("update mydb.listing " +
"set status =2 where id = $1 RETURNING id",soldInfo.listingId,cb);
}
],cb);

Related

node.js warning when calling to function

I've the following code which works
if (fs.existsSync(dirName)) {
__writeFile();
return;
}
function __writeFile() {
writeIntoFile(readStream, destFileName, reject);
numOfFiles++;
}
But after execute it im getting warning:
(node:3547) [DEP0013] DeprecationWarning: Calling an asynchronous
function without callback is deprecated.
Any idea how to avoid this ?
update this is all the code
yauzl.open(filePath, (err, zipFile) => {
if (err) {
__rejectAndLog(err);
return;
}
zipFile.on('entry', __processEntry.bind(null, zipFile))
.once('error', __rejectAndLog)
.once('close', () => {
resolve();
});
});
function __processEntry(zipFile, entry) {
if (/\/$/.test(entry.fileName)) {
return;
}
zipFile.openReadStream(entry, (err, readStream) => {
if (err) {
__rejectAndLog(err);
return;
}
//Path to drop the files
let destFileName = '/' + entry.fileName;
let dirName = path.join(res);
if (fs.existsSync(dirName)) {
__writeFile();
return;
}
mkdirp(dirName, (err) => {
if (err) {
__rejectAndLog(err);
} else {
__writeFile();
}
});
function __writeFile() {
writeIntoFile(readStream, destFileName, reject);
numOfFiles++;
}
});
}
as requested this is the content of write into file
function writeIntoFile(readStream, filePath, onError) {
if (fs.existsSync(filePath)) {
fs.chmod(filePath, '777');
}
var ws = fs.createWriteStream(filePath);
ws.on('error', function (error) {
onError(error);
});
readStream.pipe(ws);
};
update2
I try to change it according the answer below and its not working (and where should I put the mode 777)
function writeIntoFile(readStream, filePath, onError) {
if (fs.existsSync(filePath)) {
fs.chmod(filePath, 0o777, function (err) {
var ws = fs.createWriteStream(filePath);
ws.on('error', function (error) {
onError(error);
});
readStream.pipe(ws);
});
}
};

simple function not being reconized

the below function is generating the "x" is not a function error, for the love of me I have no idea why this is happening? any help is much appreciated.
function updateShareholder() {
var date = moment().format('MM/DD/YYYY');
console.log('updateShareholder');
var data = {
companyID: agreement.applicant.applicantCompanyID,
userID: agreement.coSigner.coSignerID,
agreementID: agreement.agreement.agreementID,
stock: agreement.stock.stock
}
company_worker.updateShareholder(data, function(err, result) {
if (err) {
console.log(err);
res.send(err);
} else {
console.log('updateShareholder');
process.nextTick(function() {
emailNotification()
});
}
});
};
if it helps here is the company worker function that is called within.
module.exports.updateShareHolder = function(req, callback) {
console.log('updateShareHolder');
Company.update({
"_id": req.companyID,
"shareHolders.userId": req.userID
}, {
$push: {
"shareHolders.$.agreements": {
agreementID: req.agreementID
}
}
}, {
$set: {
"shareHolders.$.shares": ++req.shares
}
},
function(err) {
if (err) {
console.log(err);
callback(err, err);
} else {
console.log('updateShareHolder');
callback(null, 'success');
}
})
};
and this is the function that calls the so-called broken function
function moveOn() {
if (addShareHolder == 'true') {
process.nextTick(function() {
addShareholder()
});
} else if (updateShareholder == 'true') {
process.nextTick(function() {
updateTheShareholder()
});
}
};
There's a typo in the function name.
You are exporting module.exports.updateShareHolder and calling company_worker.updateShareholder. Notice the lowercase h,

How return callback from a query made with node-oracledb?

The following code is the same code used in the node-oracledb GitHub examples, called select1.js. I just modified it a little bit.
module.exports = function() {
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
this.queryDB = function (query) {
oracledb.getConnection({
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
}, function(err, connection) {
if (err) {
console.error(err.message);
return;
}
connection.execute(query, function(err, result) {
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
console.log(result.metaData);
console.log(result.rows);
doRelease(connection);
return result.rows
});
});
function doRelease(connection) {
connection.release(function(err) {
if (err) {
console.error(err.message);
}
});
}
}
}
This can be used as follow:
require('./dbquery.js')();
console.log(queryDB("SELECT * FROM users"));
I expected to see the same 2D matrix (representing the table) as on line "console.log(result.rows);". But the "console.log(queryDB("SELECT * FROM users"));" returns "undefined".
How can I return a value that I get in the callback function?
I tried to add a variable X at the beginning, like this:
module.exports = function() {
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
this.queryDB = function (query) {
var X;
oracledb.getConnection({
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString
}, function(err, connection) {
if (err) {
console.error(err.message);
return;
}
connection.execute(query, function(err, result) {
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
console.log(result.metaData);
console.log(result.rows);
doRelease(connection);
X = result.rows
});
});
function doRelease(connection) {
connection.release(function(err) {
if (err) {
console.error(err.message);
}
});
}
return X;
}
}
But this is still undefined. How can I achieve this ?
It's running in async nature. You can resolve it with callback or promises. You can't get value like this.
pass the callback and return with callback
module.exports = function(callback) {//pass callback function and return with this
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
this.queryDB = function(query,callback) {
oracledb.getConnection({
user: dbConfig.user,
password: dbConfig.password,
connectString: dbConfig.connectString
}, function(err, connection) {
if (err) {
console.error(err.message);
return callback(err);
}
connection.execute(query, function(err, result) {
if (err) {
console.error(err.message);
doRelease(connection);
return;
}
console.log(result.metaData);
console.log(result.rows);
doRelease(connection);
return callback(null, result.rows)
});
});
function doRelease(connection) {
connection.release(function(err) {
if (err) {
console.error(err.message);
return callback(err);
}
});
}
};
};

Node js Query Async Map function is call

function(dataValue, cb) {
req.app.db.models.User.find({
_id: { $ne: dataValue._id }
}, function(err, totalUser) {
if (!err) {
var len = totalUser.length;
if (len !== 0) {
req.app.utility.async.map(totalUser, function(each, callback) {
console.log(each);
req.app.utility.async.mapSeries(each.nonregisterContact, function(element, callback1) {
console.log('element', element.number);
console.log('dataValue', dataValue.mobileNumber);
console.log('kolka', Number(element.number) === Number(dataValue.mobileNumber));
if (Number(element.number) === Number(dataValue.mobileNumber)) {
each.registerContact.push(dataValue._id.toString());
each.nonregisterContact.splice(element, 1);
each.save(function(err, finalResult) {
if (!err) {
} else {
console.log(err);
}
})
callback1(null, null);
} else {
callback1(null, null);
}
}, function(err, final) {
if (!err) {
callback(null, null);
} else {
console.log(err);
}
});
}, function(err, result) {
if (!err) {
console.log('2');
return cb(null, dataValue);
} else {
console.log(err);
}
});
} else {
return cb(null, dataValue);
}
} else {
cb(err);
}
})
}
I don't get any response after each.save method call in the mapSeries method final callback.I am trying this solution.How i will do the same thing. How I resolve that and handle this kind of situation?
I tried to simplify code, but I'm not sure that my code realizes your needs. Also I cann't test it :D
dataValue, each, element, finalResult are very common names, so you should use them with caution to keep code is readable/supportable.
// very bad idea is include other libraries to app
var async = require('async');
var db = require('db'); // this module must export connection to db
...
function (dataValue, cb) {
// processUser use data from closure of current function => inside of current
function processUser (user, callback) {
async.mapSeries(user.nonregisterContact, function(contact, callback){
// Check and exit if condition is not satisfied. It's more readable.
if (Number(contact.number) !== Number(dataValue.mobileNumber))
return callback(null); // ignore user
user.registerContact.push(dataValue._id.toString());
user.nonregisterContact.splice(contact, 1);
user.save(function(err, finalResult) { // Is finalResult ignore?
if (err)
console.log(err);
callback(); // ingnore error
})
}, callback);
db.models.User.find({_id: { $ne: dataValue._id }}, function(err, userList) {
if (!err)
return cb(err);
if (userList.length == 0)
return cb(new Error('Users not found'));
// use named function to avoid stairs of {}
async.map(userList, processUser, cb);
})
};

DB query is returning undefined value in node.js with oracledb

i am new to node.js and javascript and trying to learn the things. in my tests i need to pick a value from Oracle DB through select query and need to use it in to my code later. i am referring the same code given on https://blogs.oracle.com/opal/entry/introducing_node_oracledb_a_node and it is working fine but am not able to return the result value.
below is my code :
this.getTableData = function(){
var res;
oracledb.getConnection(
{
user : "user",
password : "password",
connectString : "db "
},
function (err, connection) {
if (err) {
console.error(err);
console.log("errorrrrrrrrrrr : "+err);
return;
}
connection.execute("SELECT query",
function(err, result) {
if (err) {
console.error(err);
return;
}
else if(result) {
res = result.rows[0][0];
console.log("result in else if: "+res);
return res;
}});
});
};
the function returns undefined value.
Of course it returns undefined. It's because of async callback functions. You'll need to do something like this:
this.getTableData = function(callback){
oracledb.getConnection(
{
user : "user",
password : "password",
connectString : "db "
},
function (err, connection) {
if (err) {
console.error(err);
console.log("errorrrrrrrrrrr : "+err);
return;
}
connection.execute("SELECT query",
function(err, result) {
if (err) {
console.error(err);
return;
}
else if(result) {
var res = result.rows[0][0];
console.log("result in else if: "+res);
callback(res);
}});
});
};
getTableData(function (result) {
console.log(result);
});
The other way you could solve this problem is using a Promise:
this.getTableData = function () {
return new Promise(function (resolve, reject) {
oracledb.getConnection(
{
user: "user",
password: "password",
connectString: "db "
},
function (err, connection) {
if (err) {
console.error(err);
reject(err);
console.log("errorrrrrrrrrrr : " + err);
return;
}
connection.execute("SELECT query",
function (err, result) {
if (err) {
console.error(err);
reject(err);
return;
}
else if (result) {
var res = result.rows[0][0];
console.log("result in else if: " + res);
resolve(res);
}
});
});
});
};
getTableData()
.then(function (result) {
console.log(result);
});
The code you've asked for in your comment:
var AddPage = function () {
var self = this;
this.enterOtpInput = element(by.model("beneDetail.otp"));
this.enterMpinInput = element(by.model("retailerMpin"));
this.verifyBeneficiaryButton = element(by.xpath("//div[2]/div/button"));
this.verifyBene = function () {
support.getTableData()
.then(function (result) {
console.log("adam: " + result);
self.enterOtpInput.sendKeys(result);
self.enterMpinInput.sendKeys("1111");
self.verifyBeneficiaryButton.click();
});
};
}

Categories