Friday 14 April 2017

Recover an object requested with MongoDB Driver on node JS

I try to recover object from a mongo DB database, in a node JS file and it doesn't work.

In a file called db.js , i have made the following code :

var MongoClient = require('mongodb').MongoClient;

module.exports = {
  FindinColADSL: function() {
    return MongoClient.connect("mongodb://localhost/sdb").then(function(db) {
      var collection = db.collection('scollection');

      return collection.find({"type" : "ADSL"}).toArray();
    }).then(function(items) {
      return items;
    });
  }
};

And, I try to use it in the file server.js :

var db = require(__dirname+'/model/db.js');

var collection = db.FindinColADSL().then(function(items) {
 return items;
}, function(err) {
  console.error('The promise was rejected', err, err.stack);
});

console.log(collection);

In the result I have "Promise { }". Why ?

I just want to obtain an object from the database in order to manipulate it in the others functions situated in the server.js file.



via Simon Girard

No comments:

Post a Comment