I am currently trying to create a discord bot that takes some data from the user and stores it using mongodb atlas. That works fine and when i try to do some database operations like .save() and .find() it works in the main file. But when i try to make another node project and get an access to the database and use .find(), it says : Problem.find() buffering timed out after 10000ms.
Main bot code :
require('dotenv').config()
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose')
mongoose.set('strictQuery', false);
app.use(express.json())
app.use(express.urlencoded({ extended : true }));
const uname = process.env.uname;
const password = process.env.password;
const url = `mongodb+srv://${uname}:${password}@panchofridayhai.a9fbhvv.mongodb.net/?retryWrites=true&w=majority`;
const port = process.env.PORT || 8000;
const Problem = require('./models/problems')
const token = process.env.token;
const { Client, IntentsBitField } = require('discord.js');
const problems = require('./models/problems');
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent
]
})
const start = async() => {
try {
await mongoose.connect(url);
app.listen(port, () => {
let date_object = new Date();
console.table({status : "running", time : date_object.toLocaleTimeString()});
});
}
catch(err) {
console.log(err);
}
}
start();
function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
client.on('messageCreate', (message) => {
if(message.author.bot) return;
message.reply("My name is Mocku, I can prepare mock tests for you.")
})
app.get('/', async (request, response) => {
const result = await Problem.find();
response.json({"problems" : result});
})
let id__, already = false;
client.on('interactionCreate', (interaction) => {
if(interaction.isChatInputCommand()) {
if(interaction.commandName == "create") {
if(already) {
interaction.reply("You restarted the creation process by typing /create again....\nInitializing....")
id__ = makeid(15);
return;
}
already = true;
interaction.reply("Initializing....\nType /create to start the process and /add to add a problem\nType /create again to repeat the process")
id__ = makeid(15);
}
if(interaction.commandName === "add") {
let problemStatement = interaction.options.get('problem_statement').value;
let option_one = interaction.options.get('option1').value;
let option_two = interaction.options.get('option2').value;
let option_three = interaction.options.get('option3').value;
let option_four = interaction.options.get('option4').value;
// console.log(problemStatement);
const newProblem = new Problem({
id: id__,
statement: problemStatement,
option1: option_one,
option2: option_two,
option3: option_three,
option4:option_four
})
newProblem.save();
interaction.reply("Your problem has been added....");
}
}
})
client.login(token);`
This is the main bot code and this code is not giving any errors, i used .find() and it works fine. But i want to use information from the database and use it in a website.
here is the code of another file:
require('dotenv').config();
const express = require('express');
const app = express();
const port = 5000;
const path = require('path');
const mongoose = require('mongoose')
mongoose.set('strictQuery', false);
const Problem = require('../Mocku/models/problems');
const uname = process.env.uname;
const password = process.env.password;
const url = `mongodb+srv://satyam1:0nxhzP40kXOqkyjf@panchofridayhai.a9fbhvv.mongodb.net/?retryWrites=true&w=majority`;
app.use(express.json())
app.use(express.urlencoded({ extended : true }));
app.get('/', async (request, response) => {
try {
const result = await Problem.find();
response.send({"problems" : result});
} catch (error) {
console.log(error);
}
})
const start = async() => {
try {
await mongoose.connect(url);
console.log("successfully connected to atlas");
app.listen(port, () => {
let date_object = new Date();
console.table({status : "running", time : date_object.toLocaleTimeString()});
});
}
catch(err) {
console.log(err);
}
}
start();
i saw tons of solutions but none of them worked for me. Am i making a stupid mistake ?
I tried to use .find() and .save() in another node project but it says : Problem.find() buffering timed out after 10000ms
also in the second node project i am using the username satyam1 and password is safely generated. This profile is also allowed to connect to that cluster
Top comments (0)