[英]mongoose not connected to mongodb server
Mongoose is not connected to MongoDB server Mongoose未连接到MongoDB服务器
Made sure password and username is correct 确保密码和用户名正确
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
const uri ="mongodb+srv://halbr:password@cluster0-t2noa.mongodb.net/test?retryWrites=true&w=majority"
mongoose.connect(uri).then(
() => {
console.log('Connect to Mongo');
},
err => {
console.log("Error connecting to Mongo:")
console.log(err);
}
);
module.exports = mongoose.connection
Can you please try this sample : 您可以尝试以下示例:
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
async function myDbConnection() {
const url = 'mongodb+srv://halbr:password@cluster0-t2noa.mongodb.net/test?retryWrites=true&w=majority';
try {
let connectionPromise = await mongoose.connect(url, { useNewUrlParser: true });
if (mongoose.connection) {
console.log('Connected Successfully')
global.connectionPromise = connectionPromise;
} else { global.connectionPromise = null;
console.log('not connected to DB') }
return connectionPromise;
} catch (error) {
console.log('Error connecting to DB ::', error);
}
}
module.exports = myDbConnection();
Please Verify that you added your public IP in IP Whitelist, for testing you can add wildcard. 请确认您在IP白名单中添加了公共IP,为了进行测试,您可以添加通配符。
0.0.0.0/0
, if it's okay you can try to connect to db through command line of it's not working I recommend to add new user with root role and check connection again. 0.0.0.0/0
,如果可以,您可以尝试通过命令行连接到db,但它不起作用。我建议添加具有root角色的新用户,然后再次检查连接。
use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
}
);
exit;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.