MongoError: there are no users authenticated MongoError: there are no users authenticated mongodb mongodb

MongoError: there are no users authenticated


Resolved it by closing the client first and then connecting to MongoDB again. This time use the new client returned by connect.

Relevant section from above code is:

...................adminDb.addUser(adminUser, adminPassword, {    roles: [{        role: "userAdminAnyDatabase",        db: "admin"    }]}).then(function (result) {    if (result && result.user) {        console.log("Admin user created successfully");        client.close(); // close the previous connection!    }    MongoClient.connect(format(authURL, encodeURIComponent(adminUser), encodeURIComponent(adminPassword)), function (err, authClient) {        if (err) throw err;        console.log('Authenticated Successfully');        const db = authClient.db() // this is important!   ....   ........


in my case this error came due to wrong db configuration.

my db was set with username and password, but mongoose tried to connect without them, so getting those errors.

const databaseUrl = auth  ? `mongodb://${username}:${password}@${host}:${port}/${name}` //if with auth  : `mongodb://${host}:${port}/${name}`; //without auth