Monday 1 May 2017

sailsjs use mongodb without ORM

I want to use mongodb with sails but without any ORM. So below is my service to connect mongodb.
Service:

//DbService.js

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

    module.exports = {
      db:function(req, res){
        var connect=MongoClient.connect("mongodb:***********").then(function (err, database) {
          if(err) console.log(err);
          else{
            database=database.db('*****');
            return connect;
          }  

        });  
      }
    }

After connection i have called it in controller, But getting TypeError: Cannot read property 'then' of undefined.

controller:

//HomeControlelr.js
    module.exports = {
            index:function(req, res){
                DbService.db().then(function(err,db) {
                    console.log(db);
                })
            }
    };



via Pritam Parua

No comments:

Post a Comment