问题连接节点与 mongodb

Posted

技术标签:

【中文标题】问题连接节点与 mongodb【英文标题】:Issue connecting node with mongodb 【发布时间】:2020-12-01 05:13:37 【问题描述】:

我刚开始构建我的第一个 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:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\node_modules\mongodb\lib\core\uri_parser.js:547:21)
    at connect (f:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\node_modules\mongodb\lib\operations\connect.js:277:3)
    at f:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\node_modules\mongodb\lib\mongo_client.js:222:5
    at maybePromise (f:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\node_modules\mongodb\lib\utils.js:662:3)
    at MongoClient.connect (f:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\node_modules\mongodb\lib\mongo_client.js:218:10)
    at f:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\node_modules\mongoose\lib\connection.js:714:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (f:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\node_modules\mongoose\lib\connection.js:711:19)
    at Mongoose.connect (f:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\node_modules\mongoose\lib\index.js:335:15)
    at Object.<anonymous> (f:\Works\Projects\Node\first_mern_app\mern-exercise-tracker\backend\server.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.

我不知道为什么会这样。这与教程中的代码完全相同。请帮忙

【问题讨论】:

我认为您需要用您的实际凭据替换usernamepassword &lt;dbname&gt;,这就是您收到错误的原因,因为没有错误捕获器。也做一个 try catch 块 是的。我将其修改为在这里发布。是的,我也在尝试捕捉它,但那只会返回一个异常,对吧? 【参考方案1】:

在连接字符串中,您错过了第一个字母“m”。这可能是个问题

【讨论】:

【参考方案2】:

使用此代码,我希望它有效。

       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")
);

【讨论】:

以上是关于问题连接节点与 mongodb的主要内容,如果未能解决你的问题,请参考以下文章

mongoDBmongoDB副本集实战

MongoDBMongoDB 性能优化 - BI查询聚合

mongodbmongodb提升

MongoDBMongoDB的一些操作命令

问题连接节点与 mongodb

MongoDBMongodb——GridFS存储