简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM