can't connect to mongodb
Answered
ayrreervae posted this in #help-forum
hello, first time trying mongodb here. i get this error when i try to run register
this is my code
i will post my client promise code below
querySrv ETIMEOUT _mongodb._tcp.main.ivn6hah.mongodb.net
this is my code
const name:string= params.get('name')?.toString()??""+ params.get('lname')?.toString??"";
const email:string= params.get('email')?.toString()??"";
const phone:string= params.get('phone')?.toString()??"";
const password:string= params.get('password')?.toString()??"";
const cpassword:string= params.get('cpassword')?.toString()??"";
if(password!=cpassword)
return;
let hashsalt;
await bcrypt.genSalt(10, (err:any, salt:string) => {
if (err) {
// Handle error
return;
}
hashsalt=salt;
});
let hash:string="";
await bcrypt.hash(password,hashsalt, (err:any, hashedpass:string) => {
if (err) {
// Handle error
return;
}
hash=hashedpass;
});
const client=await clientPromise;
const myDB = client.db("menu-manager");
const myColl = myDB.collection("users");
const user:User = {
user_id: uuidv4(),
name: name,
password: hash,
email: email,
phone: phone,
};
const result = await myColl.insertOne(user);
console.log(
`A document was inserted with the _id: ${result.insertedId}`,
);
authenticate(params)
}
i will post my client promise code below
Answered by ayrreervae
import { MongoClient, ServerApiVersion } from "mongodb"
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"')
}
const uri = process.env.MONGODB_URI
const options = {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
}
// if (process.env.NODE_ENV === "development") {
// // In development mode, use a global variable so that the value
// // is preserved across module reloads caused by HMR (Hot Module Replacement).
// let globalWithMongo = global as typeof globalThis & {
// _mongoClientPromise?: Promise<MongoClient>
// }
// if (!globalWithMongo._mongoClientPromise) {
// client = new MongoClient(uri, options)
// globalWithMongo._mongoClientPromise = client.connect()
// }
// clientPromise = globalWithMongo._mongoClientPromise
// } else {
// // In production mode, it's best to not use a global variable.
const client = new MongoClient(uri, options)
const clientPromise = client.connect()
// }
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise
17 Replies
Pteromalid wasp
Does your table exist in the db? Do you have a user model? Are you connection credentials setup correctly?
Your code doesn't really provide much info
Can you try console logging where you make your connection?
@Pteromalid wasp Does your table exist in the db? Do you have a user model? Are you connection credentials setup correctly?
yes and yes, i am connected to my cluster through mongodbcompass and it works fine
i also fixed my problem by deleting some code
imma show you why
import { MongoClient, ServerApiVersion } from "mongodb"
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"')
}
const uri = process.env.MONGODB_URI
const options = {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
}
// if (process.env.NODE_ENV === "development") {
// // In development mode, use a global variable so that the value
// // is preserved across module reloads caused by HMR (Hot Module Replacement).
// let globalWithMongo = global as typeof globalThis & {
// _mongoClientPromise?: Promise<MongoClient>
// }
// if (!globalWithMongo._mongoClientPromise) {
// client = new MongoClient(uri, options)
// globalWithMongo._mongoClientPromise = client.connect()
// }
// clientPromise = globalWithMongo._mongoClientPromise
// } else {
// // In production mode, it's best to not use a global variable.
const client = new MongoClient(uri, options)
const clientPromise = client.connect()
// }
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise
Answer
i kept commenting stuff till it worked
apparently the code specified for development mode is what was causing the error
@ayrreervae apparently the code specified for development mode is what was causing the error
Pteromalid wasp
So it's working now?
yes
i am hoping to know why the commented code doesn't work before i mark my solution as THE solution
that's more of a workaround than a solution
Pteromalid wasp
@ayrreervae It's probably because of your development mode. Your HMR can reload this module multiple times and this makes multiple instances of your mongoclient. This might cause some connection issues with Mongo. <- Not too sure if that is the case, but that is what it looks like
querySrv ETIMEOUT _mongodb._tcp.main.ivn6hah.mongodb.net
<- this indicates a network timeout issue so it could indeed be caused by multiple instances of the MongoClientthat does make a lot of sense
i will look into it later, my code works for now yay 
