Friday, 12 May 2017

mongoclient.connect method not working properly

 app.post('/upload', function(req, res) {
        var exceltojson;
        upload(req,res,function(err){
            if(err){
                 res.json({error_code:1,err_desc:err});
                 return;
            }
            /** Multer gives us file info in req.file object */
            if(!req.file){
                res.json({error_code:1,err_desc:"No file passed"});
                return;
            }
            /** Check the extension of the incoming file and 
             *  use the appropriate module
             */
            if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1] === 'xlsx'){
                exceltojson = xlsxtojson;
            } else {
                exceltojson = xlstojson;
            }
            console.log(req.file.path);
            try {
                exceltojson({
                    input: req.file.path,
                    output: null, //since we don't need output.json
                    lowerCaseHeaders:true
                }, function(err,result){
                    if(err) {
                        return res.json({error_code:1,err_desc:err, data: null});
                    } 

                        MongoClient.connect(url, function(err, db) {

                              assert.equal(null, err);
                              console.log("Connected correctly to server");

                              insertDocuments(db, function() {
                                db.close();
                              });
                            });
                            var insertDocuments = function(db, callback) {
                                  // Get the documents collection 
                                  var collection = db.collection('documents');
                                  // Insert some documents 
                                  collection.insertMany(result, function(err, result) {
                                    assert.equal(err, null);

                                    console.log("Inserted  documents into the document collection");
                                    callback(result);
                                  });
                                }



                    // res.json({error_code:0,err_desc:null, data: result});
                    res.send(result);
                    // console.log('before method');
                    // console.log(result);
                    //new







                });
            } catch (e){
                res.json({error_code:1,err_desc:"Corupted excel file"});
            }
        })

    });

in cmd its showing one connection now open but even the console.log is not even coming on the console which is written inside mongoclient.connect method. thats why insertdocuments method is also not being called which is also inside the same mongoclient.connect method.



via Sarthak Dey

No comments:

Post a Comment