我正在跟踪有关使用MongoDB作为数据库创建Fullstack项目的教程。该教程有些过时,因为MongoDB似乎更新了他们的过程,据我所知,将应用程序重命名为集群。我尝试将MongoDB连接到我的服务器,并且收到URL格式错误,无法将其解析为错误。
编辑:我删除了代码的图像
我的服务器文件:
const express = require("express");
const mongoose = require("mongoose");
require("dotenv").config({ path: "variables.env" });
mongoose
.connect(process.env.MONGO_URI)
.then(() => console.log("DB connected"))
.catch(err => console.error(err));
const app = express();
const PORT = process.env.PORT || 4444;
app.listen(PORT, () => {
console.log(`Server listening on PORT ${PORT}`);
});
我的variable.env文件:
MONGO_URI=mongodb+srv://Ernest:<password>@cluster0-ltxeh.mongodb.net/test?retryWrites=true&w=majority
错误消息:
Error: URL malformed, cannot be parsed
at module.exports (/Users/eboolo/Desktop/react-recipes/node_modules/mongodb/lib/url_parser.js:17:21)
at connect (/Users/eboolo/Desktop/react-recipes/node_modules/mongodb/lib/mongo_client.js:880:3)
at connectOp (/Users/eboolo/Desktop/react-recipes/node_modules/mongodb/lib/mongo_client.js:269:3)
at executeOperation (/Users/eboolo/Desktop/react-recipes/node_modules/mongodb/lib/utils.js:420:24)
at MongoClient.connect (/Users/eboolo/Desktop/react-recipes/node_modules/mongodb/lib/mongo_client.js:260:10)
at /Users/eboolo/Desktop/react-recipes/node_modules/mongoose/lib/connection.js:427:12
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (/Users/eboolo/Desktop/react-recipes/node_modules/mongoose/lib/connection.js:424:19)
at Mongoose.connect (/Users/eboolo/Desktop/react-recipes/node_modules/mongoose/lib/index.js:208:15)
at Object.<anonymous> (/Users/eboolo/Desktop/react-recipes/server.js:6:5)
at Module._compile (internal/modules/cjs/loader.js:774:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
at Module.load (internal/modules/cjs/loader.js:641:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
at internal/main/run_main_module.js:17:11