Node.js 如何使用mongoose连接到replicaset?

Node.js 如何使用mongoose连接到replicaset?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在尝试从mongoose连接到mongodb副本集 我可以在#1中使用下面的语法进行连接,但它会抛出errorUnhandledPromiseRejectionWarning:MongoError:not master and slaveOk=false 我查看了其他有类似错误的帖子,并尝试了#2和#3,但数据库连接完全失败,一些人能否提供如何连接到副本集而不抛出以下错误的指导 #1 mongoose .connect( "mongodb://username:pass

我正在尝试从mongoose连接到mongodb副本集

我可以在#1中使用下面的语法进行连接,但它会抛出
errorUnhandledPromiseRejectionWarning:MongoError:not master and slaveOk=false

我查看了其他有类似错误的帖子,并尝试了#2和#3,但数据库连接完全失败,一些人能否提供如何连接到副本集而不抛出以下错误的指导

#1

mongoose
  .connect(
    "mongodb://username:password@replicaset1:replicaset1port/myDB", { useNewUrlParser: true }
  )
#2:

mongoose
  .connect(
    "mongodb://username:password@replicaset1:replicaset1port,replicaset2:replicaset2port,replicaset3:replicaset3port/myDB?replicaSet=rsName", { useNewUrlParser: true }
  )
#3

    mongoose
      .connect(
        "mongodb://username:password@replicaset1:replicaset1port,replicaset2:replicaset2port,replicaset3:replicaset3port/myDB", { useNewUrlParser: true }
  )
错误:-

(node:8) UnhandledPromiseRejectionWarning: MongoError: not master and slaveOk=false
    at Connection.<anonymous> (/backend/node_modules/mongodb-core/lib/connection/pool.js:443:61)
    at Connection.emit (events.js:315:20)
    at processMessage (/backend/node_modules/mongodb-core/lib/connection/connection.js:364:10)
    at Socket.<anonymous> (/backend/node_modules/mongodb-core/lib/connection/connection.js:533:15)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
(node:8) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8) [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.
undefined
(节点:8)未处理的PromiserEjectionWarning:MongoError:非主控和从控OK=false
在连接处。(/backend/node_modules/mongodb core/lib/connection/pool.js:443:61)
在Connection.emit(events.js:315:20)
在processMessage(/backend/node_modules/mongodb core/lib/connection/connection.js:364:10)
在插座上。(/backend/node_modules/mongodb core/lib/connection/connection.js:533:15)
在Socket.emit(events.js:315:20)
在addChunk(internal/streams/readable.js:309:12)
在readableAddChunk(internal/streams/readable.js:284:9)
在Socket.Readable.push(internal/streams/Readable.js:223:10)
在TCP.onStreamRead(内部/stream_base_commons.js:188:23)
(节点:8)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志“---unhandled rejections=strict”(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (拒绝id:1)
(节点:8)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
未定义

尝试向对象添加
useUnifiedTopology:true
。并使用带有
replicaSet=name
的连接stirng,如您的一个示例所示。连接还返回一个承诺,因此至少添加
.catch(e=>console.log(e))