How should I implement asynchronous function in Javascript with node.js and Mongodb?

330
May 18, 2017, at 4:33 PM

So far I have seen that there are setTimeout(),process.nextTick(function) for asynchronous function however I have no clue as to how to use it. I have tried on my code since I need a asynchronous function right now but it will have Can't set headers after they are sent.

This is my code:

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';
var updateRecord = function(db, req, callback) {
db.collection('documents').updateMany({ 'Item Description': 
req.body.itemDescrip }, {
    $set: { 'Issued QTY': req.body.issueQty }
}, function(err, results) {
    if (err) return callback(err);
    console.log('Done');
    console.log(results);
    var cursor = db.collection('documents').find({
        'Item Description': req.body.itemDescrip,
        'Issued QTY': req.body.issueQty
    });
    var temp = [];
    cursor.each(function(err, doc) {
        if (err) {
            return callback(err);
        } else{
        console.log('Successfully queried');
        console.log(doc);
        temp.push(JSON.stringify(doc));
        }
    });
    callback(null, temp);
});
};
 module.exports = {
    postCollection: function(req, res) {
    var issueQty = req.body.issueQty;
    var itemDescrip = req.body.itemDescrip;
    MongoClient.connect(url, function(err, db) {
        if(err) {
          res.send(err);
          res.end();
          db.close();
          return;
        }
        updateRecord(db, req, function(err, doc) {
            if(err){
              res.send(err);
            }
            else{
              //setTimeout(function(){
                  res.send(doc);
            //},2000);
            }
            res.end();
            db.close();
        });
    });
 }
}

Basically I am trying to implement an asynchronous function such that it will execute the updateRecord first before sending a response down below as right now without the setTimeout, all I get it [] due to executing both simultaneously. This is for my web api which I am testing via postman. Any help is appreciated, thanks!

Update: Finally it worked! However my output has \included in it which is rather weird since I did not put \in the db. This is according to Roman Maksimov code below.

[
  "{\"_id\":\"18A\",\"Inventory for NTA SAM Cards\":\"\",\"Item Description\":\"Auresys EZL Prod\",\"Corrupted QTY\":\"\",\"Closing QTY\":\"100\",\"Remarks\":\"\",\"Opening QTY\":\"1000\",\"Issued QTY\":\"100\"}",
  "{\"_id\":\"20\",\"Inventory for NTA SAM Cards\":\"\",\"Item Description\":\"Auresys EZL Prod\",\"Corrupted QTY\":\"\",\"Closing QTY\":\"100\",\"Remarks\":\"\",\"Opening QTY\":\"1000\",\"Issued QTY\":\"100\"}",
  "null"
]
Answer 1

cursor.each is asynchronous, so, having the callback inside it may achieve unexpected results.

An easier way to achieve what you want is to remove the each function from the cursor and use the toArray method of mongodb. Add the final callback inside the toArray callback

var cursor = db.collection('documents').find({
  'Item Description': req.body.itemDescrip,
  'Issued QTY': req.body.issueQty
}).toArray(function(err, docs) {
  if (err) {
    return callback(err);
  }
  return callback(null, docs)
});
Answer 2

Seems your are do it under the Node.js (Am I right?). From your desciription it's not clear what are you really want to reach. But, obviously, you can't send a body to a response since you have ended it (from this point the response is already sent to the client and, of course, it's not possible to write to it). Try this:

updateRecord(db, req, function(err, doc) {
    if(err){
        res.send(err);
        res.end();
    } else {
        setTimeout(function(){
            res.send(doc);
            res.end();
        }, 2000);
    }
    db.close();
});
Rent Charter Buses Company
READ ALSO
How to find out which node package is better in performance

How to find out which node package is better in performance

I have a large xml which needs to be processed to extract a small amount of informationI found two packages which can parse the xml tag-by-tag

295
Facing error in npm install on aws ubuntu

Facing error in npm install on aws ubuntu

I'm trying to deploy my angular 2 application on aws ubuntuI have successfully install npm on ubuntu

333
passport.authenticate('jwt', {session:false}) is not working on get call

passport.authenticate('jwt', {session:false}) is not working on get call

When I register the new user the response that I get is a jwt token,I store that token in a local storage of Ionic2

369
Mongod crashe with backtrace with ***aborting after invariant() failure

Mongod crashe with backtrace with ***aborting after invariant() failure

I am facing this issue while starting server,the mongod crashes with the below mension backtrace, To resolve this everytime I have to clear /data/db folder and restore the databaseHow do I resolve this issue?

574