search

Home  >  Q&A  >  body text

Can't connect to my mongodb and backend server

Throw new error_1.MongoAPIError('URI must contain host name, domain name and tld');

Hi everyone, I'm getting an error while connecting my backend to mongodb. I'm a student and I'm learning full stack while developing a project, but I'm getting the above error.

I'm trying to connect my mongodb to my backend server but it won't happen and instead it sends me the error 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')
})

P粉426906369P粉426906369318 days ago418

reply all(1)I'll reply

  • P粉445750942

    P粉4457509422024-02-26 16:52:47

    Your password has a special character, @. You need to encode it as @.

    Change the 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
    }

    reply
    0
  • Cancelreply