【问题标题】:Unable to connect to MongoDB Atlas using Mongoose in Express无法在 Express 中使用 Mongoose 连接到 MongoDB Atlas
【发布时间】:2019-12-30 13:14:30
【问题描述】:

我正在尝试使用 mongoose 连接到 MongoDB atlas 但总是出错,

我已尝试更改我的 IP 地址并尝试更改链接 还更改了 mongoDB altas 中的所有设置,但总是得到相同的错误

const express = require('express')
const graphqlHTTP = require('express-graphql')
const app = express()
const catSchema = require('./Schema/categorySchema')
const mongoose = require('mongoose')

// SetUp MongoDB 
mongoose.connect('mongodb+srv://khawar111:khawar111@e-selling-bh1wv.mongodb.net/test?retryWrites=true&w=majority', { useNewUrlParser: true })
mongoose.connection.once('open', () => {
    console.log('Database Connected')
})
mongoose.connection.on('error', (e) => {
    console.log(e)
})

// Set Route for GraphQL 

app.use('/graphQL', graphqlHTTP({
    schema: catSchema,
    graphiql: true
})

)


app.listen(3000, () => {
    console.log('Server Started')
})

错误:

Error: queryTxt ETIMEOUT e-selling-bh1wv.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19) {
  errno: 'ETIMEOUT',
  code: 'ETIMEOUT',
  syscall: 'queryTxt',
  hostname: 'e-selling-bh1wv.mongodb.net'
}
(node:25313) UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT e-selling-bh1wv.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19)
(node:25313) 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: 1)
(node:25313) [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.

【问题讨论】:

    标签: express mongoose graphql


    【解决方案1】:

    实际上,您需要从连接字符串中删除“ test?retryWrites=true&w=majority ”并将其替换为您的数据库名称。像这样

    mongoose.connect('mongodb+srv://khawar111:khawar111@e-selling-bh1wv.mongodb.net/DatabaseName', { useNewUrlParser: true })
    

    这是因为“test?retryWrites=true&w=majority”用于测试目的

    【讨论】:

    • 您是否将所有 IP 地址列入白名单,否则您将无法连接到您的数据库。另外,请交叉检查您的用户名和密码。IP 白名单在 atlas 设置中
    • 找到解决方案,问题是我使用的是旧版本的节点驱动程序
    【解决方案2】:

    确保将节点版本更改为 2.2.12

    并添加如下 IP 地址:

    【讨论】:

      【解决方案3】:

      我遇到了这个问题,我试图找到答案,但没有任何效果,然后过了一段时间它又可以正常工作了。我认为这只是一个连接限制问题。

      【讨论】:

        猜你喜欢
        • 2020-02-22
        • 2020-07-25
        • 2019-09-04
        • 1970-01-01
        • 2017-09-09
        • 2018-08-01
        • 2020-05-12
        • 2019-03-14
        • 2022-08-02
        相关资源
        最近更新 更多