(Before starting, I should note I am very new to MongoDB and using the drivers.)
Hello! I am receiving this error when trying to run my discord bot - or application, if you will.
MongoError: MongoClient must be connected before calling MongoClient.prototype.db
I’m not quite sure why this is. Originally, I thought it was simply not connecting fast enough before MongoClient was called, so I set it up on a VPS using Redhat with AWS. However, the error still persists, so I assume it has something to do with my code.
Here is the entirety of my code where the error lies:
require(“dotenv”).config({path: “path/to/.env”});
const uri = process.env.MONGOURL;
const MongoClient = require(“mongodb”).MongoClient;
const mongo = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
});
let bname = “Beyblade”
mongo.connect(err => {
console.log(“MongoDB connected for Beyblade.js”);
});
const ids = mongo.db(“main”).collection(“ids”)
const id = ids.find({});
const datas = {};
Promise.all([id]).then(data => {
let beys = data[0];
beys.forEach(bey => {
datas[bey._id] = { latest: bey.latest, name: bey._id }
});
console.log(“Updated data!”);
});
setInterval(() => {
mongo.db(“main”).collection(“ids”).updateOne({_id: bname}, {$set: {latest: datas[bname].latest}});
}, 600000);
class Beyblade {
constructor(name, type, image, firstOwner, id){
this.name = name; this.type = type; this.image = image; this.firstOwner = firstOwner; this.level = 1; this.xp = 0; this.specials = []; this.passives = []; this.aliases = []; this.gen = 1; bname = name || this.name; if(id) this.id = id; else { if(this.name !== "Buddy Bey"){ if(datas[this.name]){ this.id = datas[this.name].latest || 1; datas[this.name].latest = (datas[this.name].latest || 1) + 1; }else{ mongo.db("main").collection("ids").insertOne({_id: this.name, latest: 2}); datas[this.name] = {latest: 2}; this.id = 1; }
ids.updateOne({_id: this.name}, {$set: {latest: datas[this.name].latest}});
}
}
}
async init(){
return true;
}
}
module.exports = Beyblade;
I hope you guys can help me out! Thank you in advanced!