Variable not retaining value outside of function - javascript

I am attempting to pass the data variable from the function to the app.get call.
But the global variable 'data' in my function is not retaining the value returned from the database query. I believe this to be a scoping issue. I set the value for 'data' to null at the beginning to make the variable accessible throughout the function. I am not hitting either of the error conditions.
function getCommands(query) {
var data = null;
try {
pg.connect(cString, function(err, client, done) {
// Catch any connection errors, and display them to the screen
if(err) {
return console.error('could not connect to postgres', err);
}
client.query(query, function(q_err, result) {
// Release the client back to the pool
done();
if(q_err) {
return console.error('error running query', q_err);
}
// Data object with an empty array to hold the row information
data = {"data":[]};
for (var row in result.rows)
{
//console.log(result.rows[row]);
data.data.push(result.rows[row]);
}
//Here, data, has the correct return values.
console.log(data);
});
});
}
catch( e )
{
console.log(e);
}
//Here, data, is null.
console.log(data);
return data;
}
app.get('/clients/', function(req, res) {
res.send(getCommands('SELECT clientid, clientname FROM hourglass.clients ORDER BY clientid ASC'));
});
Could someone help me determine why 'data' is not retaining value outside of the pg.connect function?

I think your problem is not that the variable is not retaining the value outside of the function, but rather that you console.log(data) is executing before the variable is set.
If you were to put console.log('step X') in your code as in the following example you'll see the sequence in which your code is executed.
function getCommands(query) {
var data = null;
console.log('STEP 1');
pg.connect(cString, function(err, client, done) {
console.log('STEP 3');
});
console.log('STEP 2');
}

function getCommands(query, callback) {
try {
pg.connect(cString, function(err, client, done) {
// Catch any connection errors, and display them to the screen
if(err) {
return console.error('could not connect to postgres', err);
}
client.query(query, function(q_err, result) {
// Release the client back to the pool
done();
if(q_err) {
return console.error('error running query', q_err);
}
// Data object with an empty array to hold the row information
var data = {"data":[]};
for (var row in result.rows)
{
data.data.push(result.rows[row]);
}
callback(data); //After data is set, the value is passed back
});
});
}
catch( e )
{
console.log(e);
}
}
app.get('/clients', function(req, res) {.....])
Using a callback function mentioned by #dystroy worked perfectly.

Related

Nodejs and Mongoose wait till document is saved before callback

The following function takes an aggregate cursor containing several arrays of user Ids so the users with the corresponding ids can be modified and then saved.
The issue I am having is getting the function to only return once all the users have been modified and saved. Currently it doesn't do that and when I attempt to access the users later on (literally after this function) the modifications haven't been permeated through.
This is the current code that does the modification (might be some strange async additions as I have been attempting 10s of different options with no success):
function updateUsers(dataArray, done) {
dataArray.eachAsync(
function(data) {
let userArray = data.users;
async.forEach(userArray, function(userId, callBack){
mongoose.model('User').findById(userId, function(err, user) {
// User document modified
user.save(function(err, user){
callBack();
});
});
});
},
function(err) {
//Only come here once all the users have been modified and saved!
if (err) {
return done(err, false);
}
return done(err, true);
}
);
}
The data that is passed is:
dataArray: Aggregation cursor
Result for eachAsync e.g. data is:
{ _id: 'GroupValue',
users:
[ 5a0b2f5bcd0813adeac2435b,
5a0b2f5bcd0813adeac24357,
5a0b2f5bcd0813adeac24358 ] }
You are calling the return statement via the callback after every single iteration of the callback. Thus, you are returning userArray.length times.
If you just want to insert the data and don't want the function that's using it to know whether or not they were successful in inserting, you could just do the callback immediately before even iterating through the loop.
function updateUsers(dataArray, done) {
dataArray.eachAsync(
function(data) {
let userArray = data.users;
callBack();
async.forEach(userArray, function(userId, callBack){
mongoose.model('User').findById(userId, function(err, user) {
// User document modified
user.save(function(err, user){
//do nothing
});
});
});
},
function(err) {
//Only come here once all the users have been modified and saved!
if (err) {
return done(err, false);
}
return done(err, true);
}
);
}
Otherwise, let's say you want to only callback specifically after the last iteration. One dirty way you could do this is do a temporary variable outside of the loop, and an if statement in the loop that executes on the last iteration.
For this implementation, you must assume the function will never pass a non-zero length array of users. Otherwise, it would never run a single iteration, and not terminate. (You would want to write an if length == 0 case)
function updateUsers(dataArray, done) {
dataArray.eachAsync(
function(data) {
let userArray = data.users;
let tempVar = 0;
async.forEach(userArray, function(userId, callBack){
mongoose.model('User').findById(userId, function(err, user) {
// User document modified
user.save(function(err, user){
tempVar++;
if(tempVar == userArray.length) {
callBack();
}
});
});
});
},
function(err) {
//Only come here once all the users have been modified and saved!
if (err) {
return done(err, false);
}
return done(err, true);
}
);
}
},
function(err) {
//Only come here once all the users have been modified and saved!
if (err) {
return done(err, false);
}
return done(err, true);
}
);
}

Node js: Express js asynchronous db query execution-return results got undefiend

Just started to learn express js framework ,here is my simple database query execution part its invoked with this url localhost:3000/api/test.
db.query('SELECT * FROM user', function (error, results, fields) {
if (error) throw error;
console.log('The result is:', results[0].id);
return results;
});
Does it really asynchronous?? suppose another user request this url does he need to wait for the previous query execution??.
I've heard about async package ,but don't know how this is applicable in my case
UPDATE
I got proper result in console.log(); but when i return the result i got undefined error
Here is my model.js
module.exports = {
getUser:function () {
db.query('SELECT * FROM user', function (error, results, fields) {
if (error) throw error;
console.log('The result is: ', results[0].id);
});
}
}
From my controller.js
var model = require('../models/user.js');
module.exports = {
getData : function(req, res){
//invoke model
console.log(model.getUser());
}
}
Node is non-blocking and will serve this request as and when it's called.
If another user hits this endpoint then it will execute again regardless if the first query has completed or not (unless the SQL has locked the table, in which case all consecutive connections/queries will wait and may timeout because of it). This happens on a connection basis.
You should make sure to check your SQL server (MySQL?) configs here to make sure there are enough max_connections to be able to cope with whatever load you are expecting.
Remember that the biggest bottleneck to an application is usually the database.
Your query above will need a callback to return the data asynchronously.
db.query('SELECT * FROM user', function (error, results, fields) {
if (error) throw error;
console.log('The result is:', results[0].id);
//cb=callback function passed in to context
if (cb) cb(results);
});
Updated answer from updated question
In your model.js:
module.exports = {
getUser:function (cb) {
db.query('SELECT * FROM user', function (error, results, fields) {
if (error) throw error;
console.log('The result is: ', results[0].id);
if (cb) cb(results);
});
}
}
In your controller.js:
module.exports = {
getData : function(req, res){
//invoke model
model.getUser(function(results) {
console.log(results);
});
}
}
When you deal with callback, the safe and clean way to handle them is Promises. It's now standard in JavaScript and don't require any module.
And yes it is asynchronous. Behind, there'll be network access and dialogs with the database server. Only when they're done chatting will the callback be called.
module.exports = {
getUser: function () {
// wrap asynchronously called callback in Promise
new Promise((resolve, reject) => {
db.query("SELECT * FROM user", (error, results, fields) => {
if (error) {
reject(error); // similar to "throw"
}
else {
resolve({ results, fields }); // similar to "return"
}
});
});
}
};
How do you use it:
Vanilla notation:
// similar to "try"
model.getUser()
.then((answer) => {
console.log("answer", answer);
})
// similar to "catch"
.catch((error) => {
console.log("error", error);
});
async-await notation (only available in last versions of nodejs & browsers):
// you must be in an async environement to use "await"
async function wrapper() {
try {
var answer = await model.getUser(); // wait for Promise resolution
console.log("answer", answer);
}
catch(error) {
console.log("error", error);
}
}
// async function return automatically a Promise so you can chain them easily
wrapper();

How come when using pg if i return the result of my query from a function, it returns undefined, but if i console log it, it prints?

This is my code:
const queryFirstNames = function (qString) {
let firstNameMatches;
client.query('SELECT * FROM famous_people WHERE first_name = $1', [qString], (err, result) => {
if (err) {
return console.error('error running query', err);
}
firstNameMatches = result.rows;
return firstNameMatches;
client.end();
});
};
console.log(queryFirstNames(qString));
});
This code return undefined and doesn't end the connection with the database
But if I console.log firstNameMatches inside the function instead of returning and then just invoke the function without console logging, I get the result I want, and the database connection closes properly.
What I want to do is return the result of this query and use it in another function, so I don't want it to console log the result, but when I try to return the result, it gives me undefined, and doesn't close the database connection either.
I believe that the issue you are having is that when you are returning firstNameMatches, you are returning it just to the callback for client.query. firstNameMatches is set inside of the context of the queryFirstNames function, however you never return that back. That being said, you are also utilizing an async call to client.query. This means that when you return from the queryFirstNames function, chances are you haven't finished querying. Per this, I am pretty sure you want to utilize promises to manage this or the events like one of the other answers.
In addition to this, you are going to want to move your return to before the client.end. To play with my answer, I created a jsfiddle that you can see here. I had to create my own promise via Promise just to mock out the client executing the query.
const queryFirstNames = function (qString) {
let firstNameMatches;
client.query('SELECT * FROM famous_people WHERE first_name = $1', [qString])
.then((result) => {
firstNameMatches = result.rows;
client.end();
return firstNameMatches;
})
.then(f => {
console.log(f);
})
.catch(e => {
if (err) {
return console.error('error running query', err);
}
});
};
You are returning the call before you even execute client.end(); This way your client connection will never end. You can do something like this:
const query = client.query(
'SELECT * FROM famous_people WHERE first_name = $1',
[qString],
(err, result) => {
if (err) {
return console.error('error running query', err);
}
});
query.on('row', (row) => {
results.push(row);
});
// After all data is returned, close connection and return results
query.on('end', () => {
return res.json(results);
});

Retrieve MongoDB result outside callback function scope

I'm trying to get the document retrieved by MongoClient findOne method (in r parameter) outside the scope of callback function. How can i achieve that?
Maybe my approach to the usage of MongoDB driver for Node.js is not appropiate.
function loadUser(name) {
var result = {};
function connection(err, db) {
assert.equal(null, err);
function callback(err, r) {
assert.equal(null, err);
db.close();
result = r; // This does not work
}
db.collection('users').findOne({'user.name':name}, callback);
}
MongoClient.connect(url, connection);
return result;
}
The way you're doing it, result will not be the correct object because it's returned before MongoDB can find it and the value assigned.
You should do something like:
function loadUser(name, cb) {
function connection(err, db) {
assert.equal(null, err);
function callback(err, r) {
assert.equal(null, err);
db.close();
cb(err, r) // user
}
db.collection('users').findOne({'user.name':name}, callback);
}
MongoClient.connect(url, connection);
return;
}
And the usage of loadUser would be:
loadUser("example", function(err, user){
console.log(user);
//Now do what you need with user
});
Also notice that if you are always searching for users, it would be better to just open the connection once, and close it once application is terminated.
Your 'result' variable lives in the scope of the loadUser function. The interaction with MongoDB will be asynchronous, so by the time your callback fires, the loadUser function will have finished and the result variable will no longer exist.
You could simply move the result variable to the global scope. You will probably want to restructure your code a little too though, so that the callback notifies whatever is waiting for the 'result' to return.
You can use EventEmitter to process asynchronous tasks, which makes data flow more maintainable especially if you have a lot of conditional tasks to perform :
"use strict";
var MongoClient = require('mongodb').MongoClient;
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
var url = 'mongodb://localhost:27017/myproject';
const myEmitter = new MyEmitter();
// connect to database
var db = MongoClient.connect(url, function(err, dbs) {
if (err) {
console.log(err);
return;
}
console.log("Connected correctly to server");
db = dbs;
//get user when connected
myEmitter.emit('getUser');
});
// getUser event
myEmitter.on('getUser', function() {
db.collection('users').findOne({ 'user.name': "test" }, function callback(err, r) {
if (err) {
myEmitter.emit('processError', err);
} else {
myEmitter.emit('processResult', r);
}
db.close();
});
});
// process result event
myEmitter.on('processResult', function(result) {
//result received, process it here
console.log("result : " + result);
});
// error event
myEmitter.on('processError', function(err) {
//an error occured, process the error here
console.log("error : " + err);
});

Nodejs express and promises not doing what I expect

I am trying to build a login API using NodeJS, but my code is not doing what I expect it to. I am very new to js, promises and all so please simplify any answer if possible.
From what I can see in the output of my code, the first promise part does not wait until the function findUsers(...) is finished.
I have a routes file where I want to run a few functions sequentially:
Find if user exist in database
if(1 is true) Hash and salt the inputted password
... etc
The routes file now contains:
var loginM = require('../models/login');
var loginC = require('../controllers/login');
var Promise = require('promise');
module.exports = function(app) {
app.post('/login/', function(req, res, next) {
var promise = new Promise(function (resolve, reject) {
var rows = loginM.findUser(req.body, res);
if (rows.length > 0) {
console.log("Success");
resolve(rows);
} else {
console.log("Failed");
reject(reason);
}
});
promise.then(function(data) {
return new Promise(function (resolve, reject) {
loginC.doSomething(data);
if (success) {
console.log("Success 2");
resolve(data);
} else {
console.log("Failed 2");
reject(reason);
}
});
}, function (reason) {
console.log("error handler second");
});
});
}
And the findUser function contains pooling and a query and is in a models file:
var connection = require('../dbConnection');
var loginC = require('../controllers/login');
function Login() {
var me = this;
var pool = connection.getPool();
me.findUser = function(params, res) {
var username = params.username;
pool.getConnection(function (err, connection) {
console.log("Connection ");
if (err) {
console.log("ERROR 1 ");
res.send({"code": 100, "status": "Error in connection database"});
return;
}
connection.query('select Id, Name, Password from Users ' +
'where Users.Name = ?', [username], function (err, rows) {
connection.release();
if (!err) {
return rows;
} else {
return false;
}
});
//connection.on('error', function (err) {
// res.send({"code": 100, "status": "Error in connection database"});
// return;
//});
});
}
}
module.exports = new Login();
The output i get is:
Server listening on port 3000
Something is happening
error handler second
Connection
So what I want to know about this code is twofold:
Why is the first promise not waiting for findUser to return before proceeding with the if/else and what do I need to change for this to happen?
Why is error handler second outputed but not Failed?
I feel like there is something I am totally misunderstanding about promises.
I am grateful for any answer. Thanks.
Issues with the code
Ok, there are a lot of issues here so first things first.
connection.query('...', function (err, rows) {
connection.release();
if (!err) {
return rows;
} else {
return false;
}
});
This will not work because you are returning data to the caller, which is the database query that calls your callback with err and rows and doesn't care about the return value of your callback.
What you need to do is to call some other function or method when you have the rows or when you don't.
You are calling:
var rows = loginM.findUser(req.body, res);
and you expect to get the rows there, but you won't. What you'll get is undefined and you'll get it quicker than the database query is even started. It works like this:
me.findUser = function(params, res) {
// (1) you save the username in a variable
var username = params.username;
// (2) you pass a function to getConnection method
pool.getConnection(function (err, connection) {
console.log("Connection ");
if (err) {
console.log("ERROR 1 ");
res.send({"code": 100, "status": "Error in connection database"});
return;
}
connection.query('select Id, Name, Password from Users ' +
'where Users.Name = ?', [username], function (err, rows) {
connection.release();
if (!err) {
return rows;
} else {
return false;
}
});
//connection.on('error', function (err) {
// res.send({"code": 100, "status": "Error in connection database"});
// return;
//});
});
// (3) you end a function and implicitly return undefined
}
The pool.getConnection method returns immediately after you pass a function, before the connection to the database is even made. Then, after some time, that function that you passed to that method may get called, but it will be long after you already returned undefined to the code that wanted a value in:
var rows = loginM.findUser(req.body, res);
Instead of returning values from callbacks you need to call some other functions or methods from them (like some callbacks that you need to call, or a method to resolve a promise).
Returning a value is a synchronous concept and will not work for asynchronous code.
How promises should be used
Now, if your function returned a promise:
me.findUser = function(params, res) {
var username = params.username;
return new Promise(function (res, rej) {
pool.getConnection(function (err, connection) {
console.log("Connection ");
if (err) {
rej('db error');
} else {
connection.query('...', [username], function (err, rows) {
connection.release();
if (!err) {
res(rows);
} else {
rej('other error');
}
});
});
});
}
then you'll be able to use it in some other part of your code in a way like this:
app.post('/login/', function(req, res, next) {
var promise = new Promise(function (resolve, reject) {
// rows is a promise now:
var rows = loginM.findUser(req.body, res);
rows.then(function (rowsValue) {
console.log("Success");
resolve(rowsValue);
}).catch(function (err) {
console.log("Failed");
reject(err);
});
});
// ...
Explanation
In summary, if you are running an asynchronous operation - like a database query - then you can't have the value immediately like this:
var value = query();
because the server would need to block waiting for the database before it could execute the assignment - and this is what happens in every language with synchronous, blocking I/O (that's why you need to have threads in those languages so that other things can be done while that thread is blocked).
In Node you can either use a callback function that you pass to the asynchronous function to get called when it has data:
query(function (error, data) {
if (error) {
// we have error
} else {
// we have data
}
});
otherCode();
Or you can get a promise:
var promise = query();
promise.then(function (data) {
// we have data
}).catch(function (error) {
// we have error
});
otherCode();
But in both cases otherCode() will be run immediately after registering your callback or promise handlers, before the query has any data - that is no blocking has to be done.
Summary
The whole idea is that in an asynchronous, non-blocking, single-threaded environment like Node.JS you never do more than one thing at a time - but you can wait for a lot of things. But you don't just wait for something and do nothing while you're waiting, you schedule other things, wait for more things, and eventually you get called back when it's ready.
Actually I wrote a short story on Medium to illustrate that concept: Nonblacking I/O on the planet Asynchronia256/16 - A short story loosely based on uncertain facts.

Categories