Next.js 13 MongoDB connection failed

188 Views Asked by At

I am working on authentication using Next.JS but when I try to signin a user I am getting a mongo server error this the error that I am getting

MongoServerError: bad auth : authentication failed
      ok: 0,
      code: 8000,
      codeName: 'AtlasError',
      connectionGeneration: 0,
      [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
    }
    Operation `users.findOne()` buffering timed out after 10000ms
   

And here is the code for connection.

 import mongoose from "mongoose";

    // tracking the connection
    let isConnected = false;
    
    export const connectDB = async () => {
    mongoose.set('strictQuery', true)

    if(isConnected) {
        console.log('successfully connected to the database')
        return;
    }

    try {
        await mongoose.connect(process.env.MONGO_URI, {
            dbName: "prompt",
            useNewUrlParser: true,
            useUnifiedTopology: true
        }) 

        isConnected = true
        console.log('Mongo DB was connected!!!')

    } catch (error) {
        console.log(error)
    }
    }
0

There are 0 best solutions below