简体   繁体   English

当应用程序托管在 Heroku 上时,MongoDB 未初始化

[英]MongoDB not initializing when app is hosted on Heroku

So I'm relatively new with JavaScript programming, although I've been off and on with it as a hobby for a while now.所以我对 JavaScript 编程比较陌生,尽管我现在已经断断续续地把它作为一种爱好。 My App (Discord Bot) works completely fine when I run it from my localhost with MongoDB connected via Atlas.当我从本地主机运行它并通过 Atlas 连接 MongoDB 时,我的应用程序(Discord Bot)运行良好。 However, I want it to work continuously and so I've tried uploading my code to github and hosting it on Heroku.但是,我希望它能够连续工作,因此我尝试将我的代码上传到 github 并将其托管在 Heroku 上。 The only problem I have is that the MongoDB does not initialize when hosted on Heroku and I have no idea why.我唯一的问题是 MongoDB 在 Heroku 上托管时没有初始化,我不知道为什么。

Any help in the right direction would be greatly appreciated.任何正确方向的帮助将不胜感激。

My Code:我的代码:

const connection = require("../../mongodb").initDb;
const getDb = require("../../mongodb").getDb;

module.exports = async client => {
connection(err => {
    if (err) console.log(err);

    let db = getDb();
    db.createCollection("Users");
    const collection = db.collection("Users");
    // Rest of code
    });
   };

../../mongodb file: ../../mongodb 文件:

const mongo = require("mongodb").MongoClient;
const config = require("./config.json");
const assert = require("assert");

let _db;

module.exports = {
    initDb: function initDb(callback) {
        if (_db) {
            console.warn("Trying to init DB again!");
            return callback(null, _db);
        }
        mongo.connect(process.env.MONGOCLIENTURI, config.options, connected);
        function connected(err, dataBase) {
            if (err) {
                return callback(err);
            }
            console.log("DB initialized & Connected");
            _db = dataBase.db("Members");
            return callback(null, _db);
        }
    },
    getDb: function getDb() {
        assert.ok(_db, "Db has not been initialized. Please call init first.");
        return _db;
    },

};

Error Message:错误信息:

2020-07-11T22:07:10.646273+00:00 app[web.1]: Ready!
2020-07-11T22:07:13.149231+00:00 app[web.1]: Unhandled Rejection at: Promise {
    2020-07-11T22:07:13.149249+00:00 app[web.1]:   <rejected> AssertionError [ERR_ASSERTION]: Db has not been initialized. Please call init first.
    2020-07-11T22:07:13.149249+00:00 app[web.1]:       at getDb (/app/mongodb.js:24:10)
    2020-07-11T22:07:13.149251+00:00 app[web.1]:       at Object.run (/app/commands/all/fact.js:9:14)
    2020-07-11T22:07:13.149251+00:00 app[web.1]:       at module.exports (/app/events/guild/message.js:15:11)
    2020-07-11T22:07:13.149252+00:00 app[web.1]:       at Client.emit (events.js:315:20)
    2020-07-11T22:07:13.149252+00:00 app[web.1]:       at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    2020-07-11T22:07:13.149253+00:00 app[web.1]:       at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)

So I'm relatively new with JavaScript programming, although I've been off and on with it as a hobby for a while now.所以我对 JavaScript 编程比较陌生,尽管我现在已经断断续续地把它作为一种爱好。 My App (Discord Bot) works completely fine when I run it from my localhost with MongoDB connected via Atlas.当我从本地主机运行它并通过 Atlas 连接 MongoDB 时,我的应用程序(Discord Bot)运行良好。 However, I want it to work continuously and so I've tried uploading my code to github and hosting it on Heroku.但是,我希望它能够连续工作,因此我尝试将我的代码上传到 github 并将其托管在 Heroku 上。 The only problem I have is that the MongoDB does not initialize when hosted on Heroku and I have no idea why.我唯一的问题是 MongoDB 在 Heroku 上托管时没有初始化,我不知道为什么。

Any help in the right direction would be greatly appreciated.任何正确方向的帮助将不胜感激。

My Code:我的代码:

const connection = require("../../mongodb").initDb;
const getDb = require("../../mongodb").getDb;

module.exports = async client => {
connection(err => {
    if (err) console.log(err);

    let db = getDb();
    db.createCollection("Users");
    const collection = db.collection("Users");
    // Rest of code
    });
   };

../../mongodb file: ../../mongodb 文件:

const mongo = require("mongodb").MongoClient;
const config = require("./config.json");
const assert = require("assert");

let _db;

module.exports = {
    initDb: function initDb(callback) {
        if (_db) {
            console.warn("Trying to init DB again!");
            return callback(null, _db);
        }
        mongo.connect(process.env.MONGOCLIENTURI, config.options, connected);
        function connected(err, dataBase) {
            if (err) {
                return callback(err);
            }
            console.log("DB initialized & Connected");
            _db = dataBase.db("Members");
            return callback(null, _db);
        }
    },
    getDb: function getDb() {
        assert.ok(_db, "Db has not been initialized. Please call init first.");
        return _db;
    },

};

Error Message:错误信息:

2020-07-11T22:07:10.646273+00:00 app[web.1]: Ready!
2020-07-11T22:07:13.149231+00:00 app[web.1]: Unhandled Rejection at: Promise {
    2020-07-11T22:07:13.149249+00:00 app[web.1]:   <rejected> AssertionError [ERR_ASSERTION]: Db has not been initialized. Please call init first.
    2020-07-11T22:07:13.149249+00:00 app[web.1]:       at getDb (/app/mongodb.js:24:10)
    2020-07-11T22:07:13.149251+00:00 app[web.1]:       at Object.run (/app/commands/all/fact.js:9:14)
    2020-07-11T22:07:13.149251+00:00 app[web.1]:       at module.exports (/app/events/guild/message.js:15:11)
    2020-07-11T22:07:13.149252+00:00 app[web.1]:       at Client.emit (events.js:315:20)
    2020-07-11T22:07:13.149252+00:00 app[web.1]:       at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    2020-07-11T22:07:13.149253+00:00 app[web.1]:       at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM