Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当应用程序托管在Heroku上时,MongoDB未初始化_Javascript_Node.js_Mongodb_Heroku - Fatal编程技术网

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

Javascript 当应用程序托管在Heroku上时,MongoDB未初始化,javascript,node.js,mongodb,heroku,Javascript,Node.js,Mongodb,Heroku,所以我对JavaScript编程比较陌生,虽然我已经断断续续地把它作为一种爱好有一段时间了。我的应用程序(Discord Bot)在本地主机上运行时运行完全正常,MongoDB通过Atlas连接。然而,我希望它能够持续工作,所以我尝试将代码上传到github并将其托管在Heroku上。我唯一的问题是MongoDB在Heroku上托管时没有初始化,我不知道为什么 如能在正确的方向上给予帮助,我们将不胜感激 我的代码: const connection = require("../../m

所以我对JavaScript编程比较陌生,虽然我已经断断续续地把它作为一种爱好有一段时间了。我的应用程序(Discord Bot)在本地主机上运行时运行完全正常,MongoDB通过Atlas连接。然而,我希望它能够持续工作,所以我尝试将代码上传到github并将其托管在Heroku上。我唯一的问题是MongoDB在Heroku上托管时没有初始化,我不知道为什么

如能在正确的方向上给予帮助,我们将不胜感激

我的代码:

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文件:

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;
    },

};
错误消息:

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)
2020-07-11T22:07:10.646273+00:00应用程序[web.1]:准备好了!
2020-07-11T22:07:13.149231+00:00应用程序[web.1]:未经处理的拒绝:承诺{
2020-07-11T22:07:13.149249+00:00应用程序[web.1]:断言错误[ERR_ASSERTION]:数据库尚未初始化。请先调用init。
2020-07-11T22:07:13.149249+00:00应用程序[web.1]:在getDb(/app/mongodb.js:24:10)
2020-07-11T22:07:13.149251+00:00应用程序[web.1]:at Object.run(/app/commands/all/fact.js:9:14)
2020-07-11T22:07:13.149251+00:00应用程序[web.1]:位于module.exports(/app/events/guild/message.js:15:11)
2020-07-11T22:07:13.149252+00:00应用程序[web.1]:位于Client.emit(events.js:315:20)
2020-07-11T22:07:13.149252+00:00应用程序[web.1]:位于MessageCreateAction.handle(/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
2020-07-11T22:07:13.149253+00:00应用程序[web.1]:在Object.module.exports[作为消息创建](/app/node\u modules/discord.js/src/client/websocket/handlers/MESSAGE\u CREATE.js:4:32)

Heroku使用PostgreSQL作为免费层。您应该使用Mongodb插件