I have been looking for a solution to this error for ages and have tried all of the suggestions on this site already and still no luck. I am trying to create a website where the user can register so I want to save their credentials in a database. Here is a snippet of my Javascript code where I'm connecting to my database on Atlas:
const { MongoClient } = require("mongodb");
const dbURI = 'mongodb+srv://gettingstarted.owxpopb.mongodb.net/GettingStarted';
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Connected to MongoDB Atlas');
});
On my command line I get 'connected to MongoDB Atlas' so I know its connecting. This is my user schema:
const userSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
numberOfTickets: {
type: Number,
default: 0
}
});
But when I go to save the user's information/check them against the existing database using this code:
User.findOne({ email: newUser.email }, (err, existingUser) => {
if (err) {
// Handle error
console.error(err);
return res.status(500).send({ message: 'Internal server error' });
}
if (existingUser) {
// A user with the same email address already exists
return res.status(409).send({ message: 'Email address already registered' });
}
// If we reach here, it means the user details are not already in the database
// You can proceed with saving the user to the database
const newUser = new User(userSchema);
newUser.save((err, savedUser) => {
if (err) {
// Handle error
console.error(err);
return res.status(500).send({ message: 'Internal server error' });
} else{
res.render("home");
}
// User saved successfully
return res.status(200).send(savedUser);
});
});
I get MongoServerError: user is not allowed to do action [find] on [GettingStarted.users] . I have tried updating my monoDB access in security to readWrite, made myself admin, checked that the node.js diver I'm using is compatible, and still I get this error. Can anyone help?
I have looked at solutions already posted on here but none seem to work.
For me it occured because I did not assign role the user in mongoDB. You need to assign the user role as atlasAdmin.
I had the same issue. My mongoDB was being connected and it showed in the console but I was unable to create a user.
Error I got:
MongoServerError: user is not allowed to do action [find]
I followed the solution from the link MongoError: user is not allowed to do action
I did the following steps