抛出新的 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')
}) Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您的密码有一个特殊字符,即
@。您需要将其编码为%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 }