首页  >  问答  >  正文

无法连接我的mongodb和后端服务器

抛出新的 error_1.MongoAPIError('URI 必须包含主机名、域名和 tld');

大家好,我在将后端连接到 mongodb 时遇到错误。我是学生,在开发项目时正在学习全栈,但我遇到了上述错误。

我试图将我的 mongodb 连接到我的后端服务器,但它不会发生,而是向我发送上面提到的错误。

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粉426906369236 天前333

全部回复(1)我来回复

  • P粉445750942

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

    您的密码有一个特殊字符,即 @。您需要将其编码为 %40

    将代码更改为:

    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
    }

    回复
    0
  • 取消回复