'Aws documentdb MongoServerError: Authorization failure while connecting using mongoose

error:

    [nodemon] starting `node server.js`
    app listening on port: 5600
    /home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:203
                        callback(new error_1.MongoServerError(document));
                                ^

    MongoServerError: Authorization failure
        at Connection.onMessage (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:203:30)
        at MessageStream.<anonymous> (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/connection.js:63:60)
        at MessageStream.emit (node:events:527:28)
        at MessageStream.emit (node:domain:475:12)
        at processIncomingData (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/message_stream.js:108:16)
        at MessageStream._write (/home/ubuntu/cnox-webbackend/node_modules/mongodb/lib/cmap/message_stream.js:28:9)
        at writeOrBuffer (node:internal/streams/writable:390:12)
        at _write (node:internal/streams/writable:331:10)
        at MessageStream.Writable.write (node:internal/streams/writable:335:10)
        at TLSSocket.ondata (node:internal/streams/readable:777:22) {
    ok: 0,
    operationTime: Timestamp { low: 1, high: 1652424653, unsigned: true },
    code: 13,
    [Symbol(errorLabels)]: Set(0) {}
    }

    Node.js v17.9.0
    [nodemon] app crashed - waiting for file changes before starting...

node.js code

    const express = require("express");
    var mongoose = require("mongoose");

    mongoose.connect(
    "mongodb://dfgg:cdFJFJnnsDXC@dev-docdb-cluster-1.cluster-ct090j7ac20n.us-east-1.docdb.amazonaws.com:27017/cnox?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false",{
            ssl: true,
            sslValidate: true,
            sslCA: `/home/ubuntu/rds-combined-ca-bundle.pem`,
        }
    );

I am using this code to connect aws documentdb from node.js application. I am getting above error.

I am able to connect to doc db from shell so there is no error with credentials I am able to connect using mongodb client so something is wrong with mongoose connection.

Please take a look



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source