我的代码有问题,我在其中使用了此代码 应用程序.js:
const mongoose = require('mongoose');
const dotenv = require('dotenv');
dotenv.config()
mongoose.connect(process.env.MONGO_URL)
.then(() => console.log("DB connected!"));
mongoose.connection.on('error',function(err){
console.log("The error is: ");
});
env.txt:
MONGO_URL=mongodb+srv://blur:blur@nodeapi-pfnvf8.mongodb.net/test?retryWrites=true
PORT:8080
由于某种原因,它给了我这个奇怪的错误:
throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
^ 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 new MongooseError (C:\Users\razei\Desktop\Recat\project2\node_modules\mongoose\lib\error\mongooseError.js:14:11)
at NativeConnection.Connection.openUri (C:\Users\razei\Desktop\Recat\project2\node_modules\mongoose\lib\connection.js:465:11)
at Mongoose.connect (C:\Users\razei\Desktop\Recat\project2\node_modules\mongoose\lib\index.js:289:15)
at Object.<anonymous> (C:\Users\razei\Desktop\Recat\project2\ap.js:9:10)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3) [nodemon] app crashed - waiting for file changes before starting...
有人可以帮我吗我读了一些帖子,但没有一个有帮助
最佳答案
当您未将 .env 文件保存在根文件夹中时,也会发生这种情况。
关于javascript - Mongoose 错误: The `uri` parameter to `openUri()` must be a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202427/