#can't connect to mongodb

19 messages · Page 1 of 1 (latest)

south ridge
#

hello, first time trying mongodb here. i get this error when i try to run register

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

flat sedgeBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

crystal compass
#

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?

south ridge
#

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
#

i kept commenting stuff till it worked

#

apparently the code specified for development mode is what was causing the error

south ridge
#

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

crystal compass
#

@south ridge 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 MongoClient

south ridge
#

that does make a lot of sense

#

i will look into it later, my code works for now yay lolsob