I'm working with Javascript "classes", I have a parent "class" with a variable and a function, something like this
function WS() {
this.auth = { client: '', password: '' };
this.Execute = function(url, callback) {
soap.createClient(url, function(err, client) {
if (err) return callback(err);
return callback(null, client);
});
}
}
The "subclass" uses this function and variable, like this
function Stats() {
this.url = 'http://';
this.emailsByDate = function(params, callback) {
this.Execute(this.url, function(err, client) {
if (err) return callback(err);
client.Get(this.auth.concat(params), function(err, results) {
if (err) return callback(err);
return callback(results);
});
});
}
}
Stats.prototype = new WS;
I'm getting through this.Execute() function, but this.auth variable is undefined, why is that
The context in which you are accessing this.auth isn't your Stats function, but instead your anonymous callback function.
You could either save the value of this.auth outside the callback function:
function Stats() {
var auth = this.auth
this.url = 'http://';
this.emailsByDate = function(params, callback) {
this.Execute(this.url, function(err, client) {
if (err) return callback(err);
client.Get(auth.concat(params), function(err, results) {
if (err) return callback(err);
return callback(results);
});
});
}
}
Or you can bind the context of your callback function to that of your Stats function:
function Stats() {
this.url = 'http://';
this.emailsByDate = function(params, callback) {
this.Execute(this.url, function(err, client) {
if (err) return callback(err);
client.Get(this.auth.concat(params), function(err, results) {
if (err) return callback(err);
return callback(results);
});
}.bind(this));
}
}
Related
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);
}
});
}
};
};
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);
})
};
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();
});
};
}
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);
I have the following code:
var Company = function(app) {
this.crypto = require('ezcrypto').Crypto;
var Company = require('../models/company.js');
this.company = new Company(app);
}
// Create the company
Company.prototype.create = function (name, contact, email, password, callback) {
this.hashPassword(password, function(err, result) {
if (err) throw err;
console.log(this.company); // Undefined
this.company.create(name, contact, email, result.password, function(err, result) {
if (err) {
return callback(err);
}
return callback(null, result);
});
});
}
// Get company with just their email address
Company.prototype.hashPassword = function (password, callback) {
if(typeof password !== 'string') {
var err = 'Not a string.'
} else {
var result = {
password: this.crypto.SHA256(password)
};
}
if (err) {
return callback(err);
}
return callback(null, result);
}
module.exports = Company;
The problem is that this.company is undefined on line 11 of that code block.
I know this is not what I think, but I'm not sure how to refactor to get access to the correct this.
so theres 2 solution's to this
first the dirty one
Company.prototype.create = function (name, contact, email, password, callback) {
var that = this; // just capture this in the clojure <-
this.hashPassword(password, function(err, result) {
if (err) throw err;
console.log(that.company); // Undefined
that.company.create(name, contact, email, result.password, function(err, result) {
if (err) {
return callback(err);
}
return callback(null, result);
});
});
}
and the clean one using bind https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
Company.prototype.create = function (name, contact, email, password, callback) {
this.hashPassword(password, (function(err, result) {
if (err) throw err;
console.log(this.company); // Undefined
this.company.create(name, contact, email, result.password, function(err, result) {
if (err) {
return callback(err);
}
return callback(null, result);
});
}).bind(this));
}
You can reference this through another variable by declaring it in the Company.create scope, like this:
// Create the company
Company.prototype.create = function (name, contact, email, password, callback) {
var me = this;
this.hashPassword(password, function(err, result) {
if (err) throw err;
console.log(me.company); // Undefined - not anymore
me.company.create(name, contact, email, result.password, function(err, result) {
if (err) {
return callback(err);
}
return callback(null, result);
});
});
}
Untested, but it should work like this.