flow of program execution of javascript functions in node.js project - node.js

I'm having trouble deleting an item from MongoDB array in a node.js program and found an annoying issue in flow of program execution.
Here's the code:
productController.deleteProduct = function(req,res){
productModel.findById(req.query.id,function(err, product){
if(err){
res.send(err);
}
if(storeController.deleteStoreProduct(product,res.locals.store,req)){
product.remove(function(err){
if(err){
res.send(err);
}
res.send('product deleted successfully');
});
}
else{
res.send('delete operation failed');
}
});
}
The above function runs fine. The problem is with the below function.
The above function calls storeController.deleteStoreProduct
Here's the code for storeController.deleteStoreProduct:
storeController.deleteStoreProduct = function(product, store, req){
var isDeleted = false;
storeModel.findById(store, function(err, foundStore){
if(product.category === "electronics"){
if(product.subcategory === "mobiles"){
console.log('beginning');
storeModel.update({"storeId":"store-456"}, {'$pull': {"electronics.mobiles": mongoose.Types.ObjectId(product._id)}});
console.log('yeah done!!');
isDeleted = true;
}
}
});
if(isDeleted === true){
console.log('isdeleted: true');
return true;
}
else{
console.log('isdeleted: false');
return false;
}
}
Here in the storeController.deleteStoreProduct function I have written console.log statements just for the debugging purpose.
When I run this program what it has to do is delete a particular item from storeModel collection but instead it outputs just the console.log statements; the console.log statement above the delete statement and the one below that statement executes fine, but the actual delete statement in the middle of these both console.log statements doesn't executes and neither does it throws an error.
output:
isdeleted: false
beginning
yeah done!!
Instead of running the program from the beginning, it directly goes to last if else statement in storeController.deleteStoreProduct function.
I couldn't understand what is happening here.
more details:
arguments in the function storeController.deleteStoreProduct(product,store,req) are
1.product
this is an object and is something like this:
{
"name":"asus zenfone 2",
"category": "electronics",
"subcategory":"mobiles",
"details":{
"specs":["5mp rear cam","2gb ram",16gb rom],
}
}
2.store
store is the _id of the store object in the mongodb.
3.req
This is the request object

The problem with your code is, you are calling asynchronous function storeModel.findById inside your storeController.deleteStoreProduct function and expect storeController.deleteStoreProduct to behave like a synchronous function.
Your storeController.deleteStoreProduct function is always going to return false.
your code does not print
isDeleted: true
eventhough item gets deleted properly since code block
console.log('isdeleted: true');
return true;
never gets executed.
you cant treat storeController.deleteStoreProduct as a synchronous function. You need to treat storeController.deleteStoreProduct as an asynchronous function by either make it a function which accepts a callback or making it a promise returning function.
Hope this helps.

Related

Model.findOne() function not working when passed a variable as the value

I am new to node.js.
I am trying to create function, where a randomly generated String is queried to check if it exists or not. If it already exists, the String is randomly generated till it is unique.
let validID = false;
console.log(temp); //temp is the randomly generated String.
while(!validID){
Website.findOne({shortcut: temp},function(err,docs){
if(docs==null){
validID = true;
console.log("The shortcut for the url is" + temp);
}else{
console.log("FOUND");
temp = generateId();
}
});
}
When run, the code is stuck in an infinite while loop.
I tried to see whether the code works with a String value ( not a variable ) passed in as the query inside findOne(). It worked. I am assuming that the fact that temp is a variable is causing the problem. Can variables be passed in as a value in a query? If so, what is the correct way?
Website.findOne operates asynchronously, i.e. the callback-function you passed to it, will be run once the results from the mongodb are fetched. However, node will not be able to actually process this callback, since your callstack never gets emptied due to your while-loop. If you're interested, you can find out more about this here.
One way to solve this is to wrap your Mongo-DB call in a promise, wait for it to resolve, then return if the ID is unique and continue by calling it recursively otherwise (note that this can be highly simplified by using async/await but for understanding how this works using promised are beneficial imo):
function findIdPromise(temp) {
return new Promise((resolve, reject) => {
Website.findOne({
shortcut: temp
}, function (err, docs) {
if (err) {
return reject(err);
}
resolve(docs);
});
});
}
function getNextIsUniqueIdPromise(shortcut) {
return findIdPromise()
.then(docs => {
if (docs == null) {
return shortcut;
}
return getNextIsUniqueIdPromise(generateId());
});
}
// call it initially with
getNextIsUniqueIdPromise(firstShortcutToCheck)
.then(shortcut => {
console.log("The shortcut for the url is" + shortcut):
})
.catch(err => {
console.log("an error occured", err):
});

Javascript function using promises and not returning the correct value

As a relative beginning in Javascript development, I'm trying to understand this problem I'm encountering in a function I've built that will be a part of the code to connect to a postgreSQL database.
In this function, I'm using the knex query builder method to check if a table exists in a remote database, and this method resolves to a boolean that indicates whether the string you specified matches with a table of the same name in the database. I've a provided a sample example of the knex syntax so people better understand the function.
knex.schema.hasTable('users').then(function(exists) {
if (!exists) {
return knex.schema.createTable('users', function(t) {
t.increments('id').primary();
t.string('first_name', 100);
t.string('last_name', 100);
t.text('bio');
});
}
});
I am trying to make my function return a boolean by using the .every Array method, which checks each array and should every index pass the condition defined, the .every method should return a true value, otherwise false. I've built a function which takes an array of schema keys, or names of tables, and passes it to the .every method. The .every method then uses the knex.schema.hasTable method to return a true or false.
My concern is that through many different configurations of the function, I have not been able to get it to return a correct value. Not only does it return an incorrect value, which may have something to do with .every, which I believe can return "truthey" values, but after defining the function, I will often get a "Function undefined" error when calling it later in the file. Here is a sample of my function - again I think it is moreso my poor understanding of how returns, promises and closures are working together, but if anyone has insight, it would be much appreciated.
var schemaTables = ['posts','users', 'misc'];
// should return Boolean
function checkTable() {
schemaTables.every(function(key) {
return dbInstance.schema.hasTable(key)
.then(function(exists) {
return exists;
});
});
}
console.log(checkTable(), 'checkTable function outside');
// console.log is returning undefined here, although in other situations,
I've seen it return true or false incorrectly.
Your function is not working properly for two reasons:
You are not returning the in the checkTable function declaration, so it will always return undefined.
You should write:
function checkTable() {
return schemaTables.every(function(key) {
return dbInstance.schema.hasTable(key)
.then(function(exists) {
return exists;
});
});
}
Anyway you will not get what you want just adding return. I'll explain why in the second point.
Array.prototype.every is expecting a truthy or falsey value syncronously but what the dbInstance.schema.hasTable returns is a Promise object (and an object, even if empty, is always truthy).
What you have to do now is checking if the tables exist asynchronously, i'll show you how:
var Promise = require("bluebird");
var schemaTables = ['posts', 'users', 'misc'];
function checkTable(tables, callback) {
// I'm mapping every table into a Promise
asyncTables = tables.map(function(table) {
return dbInstance.schema.hasTable(table)
.then(function(exists) {
if (!exists)
return Promise.reject("The table does not exists");
return Promise.resolve("The table exists");
});
});
// If all the tables exist, Promise.all return a promise that is fulfilled
// when all the items in the array are fulfilled.
// If any promise in the array rejects, the returned promise
// is rejected with the rejection reason.
Promise.all(asyncTables)
.then(function(result) {
// i pass a TRUE value to the callback if all the tables exist,
// if not i'm passing FALSE
callback(result.isFulfilled());
});
}
checkTable(schemaTables, function (result) {
// here result will be true or false, you can do whatever you want
// inside the callback with result, but it will be called ASYNCHRONOUSLY
console.log(result);
});
Notice that as i said before, you can't have a function that returns a true or false value synchronously, so the only thing you can do is passing a callback to checkTable that will execute as soon as the result is ready (when all the promises fulfill or when one of them rejects).
Or you can return Promise.all(asyncTables) and call then on checkTable it self, but i'll leave you this as exercise.
For more info about promises check:
The bluebird website
This wonderful article from Nolan Lawson
Thanks Cluk3 for the very comprehensive answer. I actually solved it myself by using the .every method in the async library. But yes, it was primarily due to both my misunderstanding regarding returns and asynchronous vs synchronous.
var checkTablesExist = function () {
// make sure that all tables exist
function checkTable(key, done) {
dbInstance.schema.hasTable(key)
.then(function(exists) {
return done(exists);
});
}
async.every(schemaTables, checkTable,
function(result) {
return result;
});
};
console.log(checkTablesExist());
// will now print true or false correctly

how to stop or exit on error with NodeJs

I try to understand how I can stop or exit on error in a Nodejs route.
In the code below, I check if header UID is sent and if the fiels group is also sent.
The problem is that nodejs continues to execute the rest of the code even if I use res.end () + return; I wish that Node Js stop everything when I display an error. Perhaps because I do not know much in Node Js I myself take it badly and I have to work otherwise. Can you explain to me and give me an example of how I should do?
var uid;
var group;
if (typeof req.headers['uid'] == 'undefined' || req.headers['uid'] == '') {
res.status(404);
res.json('user_id not set');
res.end();
return;
}
else
{
uid = req.headers['uid'];
User.find({_id:uid}).exec(function(err, data)
{
if(err){
res.status(404);
res.json('user not found');
res.end();
return;
}
});
}
if (typeof req.body.group == 'undefined' || req.body.group == '') {
res.status(500);
res.json('group not defined');
res.end();
return;
}
else
{
group = req.body.group;
}
it is unclear exactly what you mean by "node js stop everything". If you want to stop the entire nodejs process, you can use process.exit().
If you're trying to keep some code after your error from executing and your error occurs in an async callback, then you can't do that. The other code has already executed.
If you want to serialize asynchronous operations so that you complete one async operation BEFORE you decide whether to start the next operation, then you will need to code that differently. You will need to execute the second block of code from within the completion callback of the first async operation.
One aspect of your code that you may not understand is that this block is asynchronous:
User.find({_id:uid}).exec(function(err, data)
{
if(err){
res.status(404);
res.json('user not found');
res.end();
return;
}
});
The callback you pass to .exec() is called sometime LATER. Meanwhile, the rest of your JS has already executed. In addition, when you do a return from within that callback that doesn't return from your outer function, it only returns from that callback function back into the bowels of .exec(). It stops any more of the callback from executing, but has no effect at all on the outer function because that outer function.
So, if you want the .find() operation to finish before you execute the rest of your code in that function, you have to put that code inside the callback function.

NodeUnit testing mysql function test always successful

I'm trying to learn how to use NodeUnit to test my NodeJs code. Ive written the following code, however each time I run the test, the result is OK: 0 assertions, no matter whether the input parameter is valid or not. Can anyone explain why this doesn't work and how I can fix it?
auth.class.js: This function accepts a user ID and returns a username.
exports.username = function(uid, callback) {
db.query('SELECT username FROM ul_logins WHERE id=?', uid, function(err, results){
if (err) throw new Error(err);
if(results.length > 0)
{
callback(null, results[0].username);
}
else
throw new Error("No results.");
});
};
authtest.js: This test will run successful every time, no matter what the uid is, and count 0 assertions.
var auth = require('./auth.class.js');
exports['username'] = function (test) {
auth.username(1, function(err, data){
return test.equal(data, "joe#example.com");
});
test.done();
};
The function you are testing is asynchronous (see the rewritten function below). You need to put the test.done() in the callback function:
exports['username'] = function (test) {
test.expect(2);
auth.username(1, function (err, data) {
test.ifError(err);
test.equal(data, 'joe#example.com');
test.done();
});
};
In your version, test.done gets called before the callback to auth.username is called. Use test.expect to tell nodeunit how many asserts it should receive before test.done is called.
You have a serious problem in your auth.username function. You should not throw in asynchronous code since a try-catch cannot be used to catch the error, leading to uncaught exceptions. You should always pass errors in asynchronous code to a callback as the first argument (convention). Your function should look like:
exports.username = function (uid, callback) {
var query = 'SELECT username FROM ul_logins WHERE id=?';
db.query(query, uid, function (err, results) {
if (err) {
return callback(err);
}
if (results.length === 0) {
return callback(new Error('No user found.'));
}
callback(null, results[0].username);
});
};
Note that errors are handled first (keeps the code neat), and callback calls in the branches are returned to avoid calling the callback multiple times (using else adds a level of indentation).
I get the impression that you are new to Node, and I remember having difficulty over this point too. Consider any function that does something over the network as asynchronous. This could take a long time, so to avoid blocking the process Node gets the OS to handle the task, and takes a callback to call at some point in the future when the OS gives it a result. This is why Node relies so heavily on callbacks. It leaves the Node process free to do other things (like call test.done in your question) while it waits. It also means that try-catch (which can only catch errors thrown in the same 'tick') no longer works. The conventional way to handle errors is to call the callback with the error as the first argument. All other arguments are for actual results.

Function returning data from db

I am a newbie to node js and I am using mongoose for my models.
I have a function namde check which has a isNameThere function which receives name string as parameter in it. It checks the Db and looks for the name string which is provided if user exist in this name this.isNameThere will return true
var check= function()
{
this.nameIsThere = false;
this.isNameThere= function(name){
userModel.find({firstname: name},function(err,result){
if(result)
{
this.nameIsThere= true;
}
})
return this.nameIsThere;
}
}
Even if the name exist as you guess the code above will return false because the nature of asynchronous programming. Is there a way to execute the return isNameThere after userModel.find executes. Or any other solution for this situation. Thanks All.
Careful with the semicolons, you forgot some. It´s also good practice in JavaScript to place opening brackets right next to the function header and not in the next line.
You can encapsulate the DB call in a function like this:
function checkForName (callback) {
userModel.find({firstname: name}, callback);
}
checkForName(function (err, result) {
if (result) {
nameIsThere = true;
//do something else
...
}
});
After all, it IS anychronous, so you will not get any synchronous return value.
There´s also another way: Promises. Some libraries for you to check out:
https://github.com/caolan/async
https://github.com/kriskowal/q

Resources