我正在使用nodeJs + MongoDB Atlas创建一个小项目(REST API)。 我能够连接到端口,但是由于某些原因,当我尝试 使用MongoDB Atlas连接失败。我已将IP地址设置为 我的IP白名单中的0.0.0.0/0。不知道为什么我会收到此错误。
ERROR ON CONNECTING TO DB
{ MongoParseError: Unescaped at-sign in authority section
at parseConnectionString (D:\PROJECTS\nodejsMongodbAtlas\node_modules\mongodb-core\lib\uri_parser.js:450:21)
at connect (D:\PROJECTS\nodejsMongodbAtlas\node_modules\mongodb\lib\operations\mongo_client_ops.js:180:3)
at connectOp (D:\PROJECTS\nodejsMongodbAtlas\node_modules\mongodb\lib\operations\mongo_client_ops.js:284:3)
at executeOperation (D:\PROJECTS\nodejsMongodbAtlas\node_modules\mongodb\lib\utils.js:420:24)
at MongoClient.connect (D:\PROJECTS\nodejsMongodbAtlas\node_modules\mongodb\lib\mongo_client.js:168:10)
at Promise (D:\PROJECTS\nodejsMongodbAtlas\node_modules\mongoose\lib\connection.js:521:12)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (D:\PROJECTS\nodejsMongodbAtlas\node_modules\mongoose\lib\connection.js:518:19)
at Mongoose.connect (D:\PROJECTS\nodejsMongodbAtlas\node_modules\mongoose\lib\index.js:263:15)
at Object.<anonymous> (D:\PROJECTS\nodejsMongodbAtlas\app.js:21:10)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
name: 'MongoParseError',
[Symbol(mongoErrorContextSymbol)]: {} }
下面是我的app.js文件
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");
const app = express();
const port = parseInt(process.env.PORT || 8080);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors({ origin: true }));
mongoose.connect(
"mongodb://username:password@cluster0-shard-00-00-uauyv.mongodb.net:27017,cluster0-shard-00-04-uauyv.mongodb.net:27017,cluster0-shard-00-02-uarsv.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true",
{ useNewUrlParser: true }
);
mongoose.connection.on("connected", () => {
console.log("connected to mongodb");
});
mongoose.connection.on("error", err => {
console.log("ERROR ON CONNECTING TO DB");
console.log(err);
});
app.listen(port, () => {
console.log("connected to localhost port:" + port);
});