我在 atlas 上创建了一个集群,并尝试使用我的节点应用程序进行连接并使用 mongoose 记录连接状态。我已将我的 ip 列入白名单并正确设置所有内容,但我不断收到UnhandledPromiseRejectionWarning
.
这是我的代码db.js
。错误抛出mongooose.connect(url, opts)
。
const mongoose = require('mongoose');
const db_connect = async () => {
const conn_string = await mongoose.connect('mongodb+srv://devjoe:
<password_hidden_delibarately>@devcamper-gs1nb.mongodb.net/devcamper?retry
Writes=true&w=majority',
{
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
});
console.log(`connection string: ${conn_string.connection.host}`);
}
module.exports = db_connect;
在server.js
文件中,我只是db_connect();
在使用 commonjs 模块导入后调用了该函数。
任何帮助将不胜感激,因为我找不到问题所在。谢谢。
如果解决方案不起作用,您也可以尝试此操作:
const mongoose = require("mongoose");
const db_connect = () => {
try {
const conn_string = mongoose.connect(
"mongodb+srv://devjoe: <*****************>@devcamper-gs1nb.mongodb.net/devcamper?retry Writes=true&w=majority",
{
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
}
);
console.log(`connection string: ${conn_string.connection.host}`);
} catch {
console.log(`not connected to : ${conn_string.connection.host}`);
}
};
module.exports = db_connect;
我刚刚在我的电脑上测试了这个解决方案,它有效!
但是,如果这些都不起作用,可以向您发送我如何进行连接。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句