连接MongoDB与NodeJS时获取ReplicaSetNoPrimary和MongoServerSelectionError错误 [英] getting ReplicaSetNoPrimary and MongoServerSelectionError error while connecting MongoDB with nodejs

查看:32
本文介绍了连接MongoDB与NodeJS时获取ReplicaSetNoPrimary和MongoServerSelectionError错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接MongoDB,但出现以下错误,请您帮助

var mongo = require('mongodb').MongoClient;
mongo.connect('mongodb://usernamexyz:passwordxyz@hostmxy-mw-e6-u1238.nam.nsroot.net:47017/sampleDB?replicaSet=NAME_2436&readPreference=primary&authSource=admin&w=1', { useNewUrlParser: true, useUnifiedTopology: true  })
.then(() => console.log("Mongodb connected"))
.catch(err => console.log(err));

和我收到的错误如下

MongoServerSelectionError: connection <monitor> to 155.30.360.129:37017 closed
    at Timeout._onTimeout (C:FintechNodeFirstApp
ode_modulesmongodblibcoresdam	opology.js:448:30)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7) {
  name: 'MongoServerSelectionError',
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map {
      'hostmxy-mw-e6-u1238.nam.nsroot.net:47017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

mongodb

转到您创建集群的推荐答案,在ipwhitelist下单击编辑IP地址按钮,然后选择Add ccurrent IP address

这对我很管用

这篇关于连接MongoDB与NodeJS时获取ReplicaSetNoPrimary和MongoServerSelectionError错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆