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.
Related
I have looked around and tried all kinds of docs to be able to get the $where clause in MongoDB to work for me, but it just won't work.
Here is my object:
var UserProfiles = [{
userProfileID: "3f8c553c-3633-4fe9-a007-4346a783450c",
firstName: 'Austin',
lastName: 'Hunter',
email: 'ahunter8....com',
token: '',
platform: '',
password: 'admin',
companyProfileID: "86660a5b-7f61-4238-889d-1cc3087947b9",
authentication: ''
}....
there are several "profiles" inserted into the UserProfiles Object. That is just the first one.
Here is me inserting into the collection:
MongoClient.connect(url, function(err, db) {
if (err) {
console.log(err);
} else {
console.log("We are connected");
}
var collection = db.collection('UserProfile');
for (var i = 0; i < UserProfiles.length; i++) {
collection.insert(UserProfiles[i], function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
}
db.close();
});
Now I am trying to search my collection for a passed in email AND companyProfileID. If they both match then return that profile. I thought the $where clause would be best but I can't get it to work.
Here is me trying to find():
function getUserProfile(passInEmail, companyID, callback) {
MongoClient.connect(url, function(err, db) {
if (err) {
console.log(err);
} else {
console.log("We are connected");
}
var collection = db.collection('UserProfile');
collection.find({$where: "this.email == passInEmail"}, function(err, result) {
if (err) {
console.log(err);
callback(err);
} else if (result.length) {
console.log(result);
callback(result);
} else {
callback(err);
console.log("No document found");
}
});
db.close();
});
}
I am trying to search the collection and if the object email matches the passed in email and the object companyProfileID matches the passed in companyID then success.
The $where clause in your case in not the best thing.
You should do simply:
//userIdParam ad emailParam are two variables
collection.find({userProfileID: userIdParam, email: emailParam})
.toArray(function(err, result) {
if (err) {
console.log(err);
callback(err);
} else if (result.length) {
console.log(result);
callback(result);
} else {
callback(err);
console.log("No document found");
}
});
Take a look of the doc here
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);
}
});
}
};
};
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 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));
}
}
I can use the following to save a new document to my mongodb database.
User.prototype.save = function (fn) {
var user = new userModel({
user: this.user,
pass: this.pass
});
console.log('user: ' +user);
this.hashPassword (user.pass, function (err, salt, hash) {
if (err) return fn (err);
this.pass = hash;
user.salt = salt;
user.pass = hash;
user.save (function (err, product, numberAffected) {
if (err) return fn (err);
return fn(undefined);
});
});
};
Now I'm trying to check if the user already exists before saving it
User.prototype.save = function (fn) {
// See if the username exists
userModel.findOne ({ 'user': this.user }, function (err, user) {
if (err) return fn (err);
if (!user) {
user = new userModel({
user: this.user,
pass: this.pass
});
console.log('user: ' +user);
this.hashPassword (user.pass, function (err, salt, hash) {
if (err) return fn (err);
this.pass = hash;
user.salt = salt;
user.pass = hash;
user.save (function (err, product, numberAffected) {
if (err) return fn (err);
return fn(undefined);
});
});
} else {
// TODO: update all the user fields
console.log ('user already exists');
}
});
}
this.hashPassword is no longer found TypeError: Object #<Promise> has no method 'hashPassword' and the fields of user are now undefined. How can I use this within these callbacks?
EDIT 1:
Taking a closer look I also notice that this.pass = hash; in the first snippet also does nothing to the object I care about.
on line 2
var self = this
then,
self.hashPassword()
In full:
User.prototype.save = function (fn) {
var self = this;
// See if the username exists
userModel.findOne ({ 'user': self.user }, function (err, user) {
if (err) return fn (err);
if (!user) {
user = new userModel({
user: self.user,
pass: self.pass
});
console.log('user: ' +user);
self.hashPassword (user.pass, function (err, salt, hash) {
if (err) return fn (err);
self.pass = hash;
user.salt = salt;
user.pass = hash;
user.save (function (err, product, numberAffected) {
if (err) return fn (err);
return fn(undefined);
});
});
} else {
// TODO: update all the user fields
console.log ('user already exists');
}
});
}