无法使用NPM连接到MongoDB。URI问题?

无法使用NPM连接到MongoDB。URI问题?,mongodb,npm,Mongodb,Npm,因此,我无法连接到MongoDB(Atlas)。感觉我可能要发疯了,因为木头就像沉船上的锚一样有用。事情是这样的谢谢你的帮助。 这是我的server.js文件: 'use strict'; const mongodb = require('mongodb'); const http = require('http'); const nconf = require('nconf'); let uri = 'mongodb://USERNAME:PASSWORD@cluster0-w4hci.mo

因此,我无法连接到MongoDB(Atlas)。感觉我可能要发疯了,因为木头就像沉船上的锚一样有用。事情是这样的谢谢你的帮助。

这是我的server.js文件:

'use strict';

const mongodb = require('mongodb');
const http = require('http');
const nconf = require('nconf');
let uri = 'mongodb://USERNAME:PASSWORD@cluster0-w4hci.mongodb.net/test?retryWrites=true&w=majority;';
以下是console所说的内容

npm start

> test@1.0.0 start /home/XXXXXXX
> node server.js

mongodb://USERNAME:PASSWORD@cluster0-w4hci.mongodb.net/test?retryWrites=true&w=majority;

/home/XXXXXXX/node_modules/mongodb/lib/mongo_client.js:421
          throw err
          ^
MongoError: failed to connect to server [cluster0-w4hci.mongodb.net:27017] on first connect [MongoError: getaddrinfo ENOTFOUND cluster0-w4hci.mongodb.net cluster0-w4hci.mongodb.net:27017]
    at Pool.<anonymous> (/home/XXXXXXX/node_modules/mongodb-core/lib/topologies/server.js:336:35)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (/home/XXXXXXX/node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/home/XXXXXXX/node_modules/mongodb-core/lib/connection/connection.js:187:49)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the test@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/XXXXXXX/.npm/_logs/2020-02-23T15_17_31_259Z-debug.log
npm启动
> test@1.0.0开始/开始/XXXXXXX
>node server.js
mongodb://USERNAME:PASSWORD@cluster0-w4hci.mongodb.net/test?retryWrites=true&w=多数;
/home/XXXXXXX/node_modules/mongodb/lib/mongo_client.js:421
失误
^
MongoError:无法在第一次连接[MongoError:getaddrinfo ENOTFOUND cluster0-w4hci.mongodb.net:27017]时连接到服务器[cluster0-w4hci.mongodb.net:27017]
在游泳池。(/home/XXXXXXX/node_modules/mongodb core/lib/topologies/server.js:336:35)
在emitOne(events.js:116:13)
在Pool.emit(events.js:211:7)
在连接处。(/home/XXXXXXX/node_modules/mongodb core/lib/connection/pool.js:280:12)
在Object.onceWrapper(events.js:317:30)
两点钟(events.js:126:13)
在Connection.emit(events.js:214:7)
在插座上。(/home/XXXXXXX/node_modules/mongodb core/lib/connection/connection.js:187:49)
在Object.onceWrapper(events.js:315:30)
在emitOne(events.js:116:13)
npm错误!代码失效循环
npm错误!错误1
npm错误!test@1.0.0start:`node server.js`
npm错误!退出状态1
npm错误!
npm错误!失败test@1.0.0开始脚本。
npm错误!这可能不是npm的问题。上面可能还有其他日志输出。
npm错误!此运行的完整日志可在以下位置找到:
npm错误/home/XXXXXXX/.npm/_logs/2020-02-23T15_17_31_259Z-debug.log

我的node.js驱动程序的版本与我想象的不同。一旦我从Atlas中选择了正确的版本,URI就工作了,我没有连接

你被列入白名单了?问得好。我允许0.0.0.0/0。还有什么我应该解锁的吗?你确定
0.0.0.0/0
是你的IP地址吗?如果没有,您可以使用以下方法检查您的IP地址:在数据库URI中,您是否故意删除了问题帖子的
用户名
密码
值,或者这正是您在计算机上运行的代码中写入的内容?是的,我添加了我的IP。以及来自GCP的IP。我故意隐藏用户名和密码。但是,真正的连接字符串具有正确的连接字符串。