passing mongoose as an argument to a function

送分小仙女□ 提交于 2019-12-02 12:40:53

Looking at the documentation of mongoose connect


You can use of Promises.

    var mongoose = require('mongoose');
    var myModule = require('myModule');

    var dbUrl = 'mongodb://localhost:27017/gfsTestDB';

    mongoose.connect(dbUrl)
      .then(
        // The connection is ready to use!
        () => {
          var readyObj = new myModule(mongoose);

          // ...
        },

        // Handle the connection error
        (err) => {
          // ...
        },
      );

You can use of Callbacks

    var mongoose = require('mongoose');
    var myModule = require('myModule');

    var dbUrl = 'mongodb://localhost:27017/gfsTestDB';

    mongoose.connect(dbUrl, (err) => {
      if (err) {
        // Handle the error

        // ...

        return;
      }

      // We get successfully connected to the database

      var readyObj = new myModule(mongoose);

      // ...
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!