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!
....
........