MongooseError:openUri()的uri参数必须是字符串

 收藏

我的代码有问题,我在 app.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...

有人可以帮我吗,我读了几篇文章,但都没有帮助

回复