【问题标题】:Unable to connect MongoDB server?无法连接 MongoDB 服务器?
【发布时间】:2020-11-03 02:18:51
【问题描述】:

我尝试使用下面提到的链接连接到 MongoDB 服务器,但出现错误,我输入了正确的密码和 dbName 但无法连接。你能帮帮我吗?

const mongoose = require('mongoose');
    mongoose.connect(`mongodb+srv://test:Test@123@cluster0.cvbne.mongodb.net/example?retryWrites=true&w=majority`,{userNewUrlParser : true, useUnifiedTopology: true},
    err => {
        if(!err)
        console.log ("'mongoddb connection successed")
        else
        console.log ('Error white connecting mongodb : ' + JSON.stringify(err, undefined,2))
    })

【问题讨论】:

    标签: javascript node.js reactjs mongodb express


    【解决方案1】:

    改用这个:

    mongoose.connect(`mongodb+srv://test:Test@123@cluster0.cvbne.mongodb.net/example?retryWrites=true&w=majority`), {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    const db = mongoose.connection;
    db.on("error", console.error.bind(console, "connection error:"));
    db.once("open", () => console.log("---Connected to DB!---"));
    

    【讨论】:

    • 非常感谢。我很感激你的回答。现在一切正常。
    猜你喜欢
    • 2019-03-02
    • 2014-02-04
    • 2021-10-03
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2014-07-23
    相关资源
    最近更新 更多