0

我刚开始构建我的第一个 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.

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

4

2 回答 2

1

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

于 2020-08-11T14:23:48.530 回答
0

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

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

于 2020-08-11T15:02:03.570 回答