繁体   English   中英

mongoDB如何正确连接?

[英]How do you properly connect mongoDB?

我有这个,但它带来了很多错误。 有什么建议么?

mongoose.connect(process.env.SRV, {
useNewUrlParser: true,
useUnifiedTopology: true, 
useFindAndModify: false
}).then(() =>{
console.log('Connected to database!')
}).catch((err) =>{
console.log(err)
});

以下是错误:

MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
at NativeConnection.Connection.openUri (C:\Code\bots\BuyBot\node_modules\mongoose\lib\connection.js:688:11)
at C:\Code\bots\BuyBot\node_modules\mongoose\lib\index.js:348:10
at C:\Code\bots\BuyBot\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Code\bots\BuyBot\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at Mongoose._promiseOrCallback (C:\Code\bots\BuyBot\node_modules\mongoose\lib\index.js:1140:10)
at Mongoose.connect (C:\Code\bots\BuyBot\node_modules\mongoose\lib\index.js:347:20)
at Object.<anonymous> (C:\Code\bots\BuyBot\index.js:17:10)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47

我不知道它为什么这么生气。

在根文件夹中创建一个 .env 文件,并在该文件中添加以下行。 不要忘记在您的应用程序中包含 dotenv package。

SRV="Your_Mongo_URI"

现在运行代码,这可能对你有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM