简体   繁体   English

如何正确连接到 mongodb.atlas? Mongo网络错误

[英]how to properly connect to mongodb.atlas? MongoNetworkError

I am learning how to use MongoDB with node.js, so I am making a restful-API as practice.我正在学习如何在 node.js 中使用 MongoDB,所以我正在制作一个 restful-API 作为练习。

But I keep getting "MongoNetworkError: getaddrinfo ENOTFOUND" whenever I try to connect to atlas for mongo.但是每当我尝试连接到 mongo 的地图集时,我都会收到“MongoNetworkError:getaddrinfo ENOTFOUND”。

My code at server.js :我在server.js代码:

// call the packages
var bodyParser = require('body-parser');
var Bear = require('./app/models/bear');
var mongoose = require('mongoose');

var express = require('express');
var app = express();

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb://<userName>:<pasword>@cluster0-<someCharacters>.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect( err => {
  console.log(err);
  //const collection = client.db("test").collection("devices");
  client.close();
});

/*rest of the code is commented out*/
...

It logs the following error:它记录以下错误:

{ MongoNetworkError: failed to connect to server [cluster0-<someCharacters>.mongodb.net:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND cluster0-<someCharacters>.mongodb.net cluster0-<someCharacters>.mongodb.net:27017]
at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:431:11)
at Pool.emit (events.js:198:13)
at connect (/app/node_modules/mongodb-core/lib/connection/pool.js:557:14)
at makeConnection (/app/node_modules/mongodb-core/lib/connection/connect.js:39:11)
at callback (/app/node_modules/mongodb-core/lib/connection/connect.js:261:5)
at Socket.err (/app/node_modules/mongodb-core/lib/connection/connect.js:286:7)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }

notes:笔记:

  • <userName> , <pasword> , <someCharacters> represent characters accordingly <userName> , <pasword> , <someCharacters>表示字符
  • I get the same results both in my laptop and in Heroku我在笔记本电脑和 Heroku 中得到相同的结果
  • If set up mongodb localy it works fine on localhost如果在本地设置 mongodb,它可以在localhost上正常工作
  • I got the conection string directly from cloud.mongodb.com我直接从 cloud.mongodb.com 得到了连接字符串

what might I be doing wrong?我可能做错了什么?

u installed mongoose but you are trying to connect with mongodb package.你安装了 mongoose,但你正在尝试连接 mongodb 包。 since you installed mongoose因为你安装了猫鼬

const mongoUri="your connection string here"
mongoose.connect(mongoUri,{useNewUrlParser:true})
.catch((e)=>{
    console.log(e.message)
    process.exit(1)
})
.then(()=>{
    console.log("connected to Mongo Atlas")
})

mongoose.connect return promise. mongoose.connect 返回承诺。 if there is an error, connection will be rejected, it will log the error and then exit the process.如果有错误,连接将被拒绝,它会记录错误,然后退出进程。 if connection is success, that means promise is resolved, so it will connect and then will log the "connected to Mongo Atlas"如果连接成功,则表示 promise 已解决,因此它将连接,然后将记录“已连接到 Mongo Atlas”

However i suggest you to check your connection string.但是我建议您检查您的连接字符串。 it should have "+srv", something like this它应该有“+srv”,像这样

  mongodb+srv://

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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