How do I get the MongoDb connection from inside Loopback.io

守給你的承諾、 提交于 2019-12-05 20:00:51

Okay, did a little more digging, mostly into the loopback and mongo connector source code. If you want to get direct access to the mongoDB connection you can, but be careful!

module.exports = function(ZipCodes) {
    ZipCodes.pipeline = function (cb) {
        //Get the MongoDB Connection
        var mongodbConnection = ZipCodes.dataSource.connector.db;
        if (!mongodbConnection) {
            // may not be connected yet, you might have to do that manually:
            // (careful! this is asynchronous)
            ZipCodes.dataSource.connect(function(err, db) {
                mongodbConnection = db;
            });
        }

        // do whatever you need to with the mongo connection...
        cb(result);
    };

    // ... other stuff

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