使用mongodb发出连接节点问题



我刚开始构建我的第一个MERN应用程序,当我试图连接到mongoDB时,我遇到了问题。这是我的代码:

const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
require("dotenv").config({ path: ".env" });
const app = express();
const port = process.env.port || 8000;
app.use(cors());
app.use(express.json());
app.listen(port, () => {
console.log(" Listening on port:  ", port);
});
const uri =
"ongodb+srv://username:password@cluster0.p95p7.gcp.mongodb.net/<dbname>?retryWrites=true&w=majority";
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });

const connection = mongoose.connection;
connection.once("open", () => {
console.log("Connected successfully");
});

当我跑步时,我得到的是:

Listening on port:   8000
(node:3972) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
at parseConnectionString (f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendnode_modulesmongodblibcoreuri_parser.js:547:21)
at connect (f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendnode_modulesmongodbliboperationsconnect.js:277:3)
at f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendnode_modulesmongodblibmongo_client.js:222:5
at maybePromise (f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendnode_modulesmongodblibutils.js:662:3)
at MongoClient.connect (f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendnode_modulesmongodblibmongo_client.js:218:10)
at f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendnode_modulesmongooselibconnection.js:714:12
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendnode_modulesmongooselibconnection.js:711:19)
at Mongoose.connect (f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendnode_modulesmongooselibindex.js:335:15)
at Object.<anonymous> (f:WorksProjectsNodefirst_mern_appmern-exercise-trackerbackendserver.js:19:10)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
(node:3972) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:3972) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我不知道为什么会发生这种事。这与教程中的代码完全相同。请帮助

在连接字符串中,您漏掉了第一个字母'm'。这可能是的问题

使用这段代码,我希望它能工作。

mongoose.connect(`mongodb://${ip}:27017/dbname`, {
"user": "username",
"pass": "password",
"useNewUrlParser": true,
"useUnifiedTopology": true,     
},(err)=>{
if(err)
console.log({err})
else
console.log("mongo connected")
});

相关内容

  • 没有找到相关文章