skip to Main Content

throw new error_1.MongoAPIError(‘URI must include hostname, domain name, and tld’);

Hello everyone i am facing a error while connecting my backend to mongodb . I am student and i am learning full stack while developing my project i am facing the above error.

I was trying to connect my mongodb with my backend server but it wont happen is send me an error that i mentioned above.

const connect = async () => {
    try{
        await mongoose.connect('mongodb+srv://rahulghatge166:Rahul@[email protected]/Booking?retryWrites=true&w=majority');
        console.log('Connected to MongoDb')
    }catch(error) {
        throw error
    }
}

mongoose.connection.on('disconnected', () => {
    console.log('mongoDb disconnected')
})
mongoose.connection.on('connected',() => {
    console.log('mongoDb is connected')
})

2

Answers


  1. Solution 1

    If you are using any special character in your password you need to encode the particular character as %+ASCII_code_of_the_character below link explains everything. https://docs.atlas.mongodb.com/troubleshoot-connection/#special-characters-in-connection-string-password

    Solution 2

    Click on auto-generate password and paste it into the connection string. It will work.

    Solution 3

    Create new password which doesn’t have special characters.

    Login or Signup to reply.
  2. Your password has a special character i.e. @. You’ll need to encode it as %40.

    Change code to:

    const connect = async () => {
    try{
        await mongoose.connect('mongodb+srv://rahulghatge166:Rahul%[email protected]/Booking?retryWrites=true&w=majority');
        console.log('Connected to MongoDb')
    } catch(error) {
        throw error
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search