简体   繁体   中英

Node/express application failing to connect to Mongodb Atlas using mongoose

I have a node/express application that I am trying to connect to Mongodb Atlas using mongoose.

All of my code is identical to a previous app that I had connect to Atlas (which worked fine). When I run it on my work machine (Windows 10) everything works as expected. However, when I run it on my MacBook Pro (Mojave), the express app runs but the mongoose connection to Atlas throws the following error:

{ Error: queryTxt EBADNAME development-zv5hp.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:196:19)
  errno: 'EBADNAME',
  code: 'EBADNAME',
  syscall: 'queryTxt',
  hostname: 'development-zv5hp.mongodb.net' }

server.js

const express = require('express');
const mongoose = require('mongoose');

const app = express();

mongoose
  .connect(
    'mongodb+srv://client:<PASSWORD>@development-zv5hp.mongodb.net/shop',
    { useNewUrlParser: true }
  )
  .then(() => console.log('MongoDB Connected...'))
  .catch(err => console.log(err));

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

What might be causing this issue? I have checked the Atlas user and password and have whitelisted my IP (in fact whitelisted all IPs)

Using:

  • node v10.15.3
  • express v4.16.4
  • mongoose v5.5.1

使用谷歌的DNS服务器8.8.8.88.8.4.4可以解决这个问题

please add autoIndex: false its working for me

mongoose
  .connect(
    'mongodb+srv://client:<PASSWORD>@development-zv5hp.mongodb.net/shop',
    {autoIndex: false, useNewUrlParser: true }

This error is caused because the URI 'mongodb+srv://client:<PASSWORD>@development-zv5hp.mongodb.net/shop' that was passed to connect was unable to be resolved. Your DNS server does not know it and thus cannot resolve an IP. Hence ebadname.

change this like Addison mentioned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM