【问题标题】:Unable to connect mongoDb to Express无法将 mongoDb 连接到 Express
【发布时间】:2022-02-20 17:27:53
【问题描述】:

我正在使用 MERN 堆栈制作注册表单,我正在将 Express 应用程序连接到 mongoDB,但连接时出现错误。 我在 YouTube 上关注 Thapa Technical video,我完全按照 Thapa 所做的那样做,我不知道这有什么问题,我是初学者,请帮助我。

app.js 文件:

const express = require('express');
const app = express();
require("./db/conn")

const port = process.env.PORT || 3000;

app.get("/", (req, res) => {
        res.send("Welcome to Abdullah's webbsite")
    });
    
    app.listen(port, () => {
            console.log(`Server is running at port no ${port}`)
        })

conn.js 文件:

const mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/userRegistration", {
    useNewUrlParser:true,
    useUnifiedTopology:true,
    useCreateIndex:true
}).then(() =>{
    console.log(`Connection Successful`);
}).catch((e) => {
    console.log(e);
})

package.json 文件:

{
  "name": "registration-form",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev" : "nodemon src/app.js"
  },
  "author": "Abdullah",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.3",
    "hbs": "^4.2.0",
    "mongoose": "^6.2.2"
  }
}

控制台:

    Server is running at port no 3000
MongoParseError: option usecreateindex is not supported
    at parseOptions (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongodb\lib\connection_string.js:289:15)
    at new MongoClient (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongodb\lib\mongo_client.js:62:63)
    at C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\connection.js:784:16
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\connection.js:781:19)
    at C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\index.js:340:10
    at C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\Users\ABDULLAH\Desktop\Registration Form\Registration form\node_modules\mongoose\lib\index.js:1140:10)

【问题讨论】:

  • 您是否尝试记录您在catch(e) 中遇到的错误?
  • 请将console.log(Not connected) 更改为console.log(e) 并发布响应,确保端口和数据库已经暴露并运行
  • @sina.ce 我已经按照你的指示更新了,mongod 正在运行

标签: javascript node.js mongodb express mongoose


【解决方案1】:

请删除connn.js 中的useCreateIndex 选项:

mongoose.connect("mongodb://localhost:27017/userRegistration", {
    useNewUrlParser:true,
    useUnifiedTopology:true
}).then(() =>{
    console.log(`Connection Successful`);
}).catch((e) => {
    console.log(e);
})

【讨论】:

    【解决方案2】:
    const express = require('express');
    const mongoose = require('mongoose');
    
    const app = express();
    app.use(express.json());
    
    
    let userModel;
    
    // configure mongoose
    mongoose.connect('mongodb://localhost:27017/userRegistration', (err) => {
      if (err) {
        console.log(`Error connecting to mongodb => `, err);
      } else {
        console.log(`Mongodb Connected successfully`);
      }
    });
    

    【讨论】:

    • 您的答案可以通过添加有关代码的作用以及它如何帮助 OP 的更多信息来改进。
    猜你喜欢
    • 2017-05-10
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2012-02-01
    • 2016-07-31
    相关资源
    最近更新 更多