Similar question has been asked many times. But i just cant figure it out. Solving this issue literally whole day. Basically, what i understood from the diffrent answers, the error means that im loading my model into the application before the mongoose connection is made. I resolved this, but its still not working.. anyone has any idea?
-- this is the index.ts file
.connect(mongoDBURL)
.then(() => {
console.log("Connecting to telegram...");
const bot = new Telegraf(telegramConnection.token);
addProperties(bot, mongo);
bot.launch();
console.log("bot is ready");
})
.catch((err) => {
console.log(err);
});
-- user.ts
bot.hears("/register", (ctx) => {
MasterController()
.userController(mongo)
.createUser(ctx.message.chat.id, "random_user");
});
-- the createUser function
const createUser = async (_chatId: Number, _username: string) => {
console.log(`chatID = ${_chatId}`);
const user = new UserModel({
chatId: _chatId,
username: _username,
});
await user
.save()
.then((res) => {
console.log("User registered");
})
.catch((err) => {
console.log(err);
});
};