How to connect to replicaset using mongoose?

Viewed 616

I am trying to connect to mongodb replica sets from mongoose following https://mongoosejs.com/docs/connections.html#replicaset_connections

I was able to connect using below syntax in #1 but it throws the errorUnhandledPromiseRejectionWarning: MongoError: not master and slaveOk=false

I looked at other posts with similar error and tried #2 and #3 but the database connection fails altogether,can some provide guidance on how to connect to replica sets without throwing the below error?

#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 }
  )

Error:-

(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
0 Answers
Related