I've been struggling with this error for quite some time now, attempting all the recommendations on this forum without success. My goal is to develop a website where users can register and their credentials are saved in a database. Below is a snippet of my JavaScript code that connects to my Atlas database:
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');
});
Upon checking the command line, it shows 'connected to MongoDB Atlas' confirming successful connection. This is my user schema description:
const userSchema = new mongoose.Schema({
name: String,
email: String,
password: String,
numberOfTickets: {
type: Number,
default: 0
}
});
However, when I attempt to save or verify a user's information against the existing database using the following 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);
});
});
It returns a MongoServerError: user is not allowed to do action [find] on [GettingStarted.users]. Despite trying various solutions like updating MongoDB security access to readWrite, giving myself admin rights, verifying node.js driver compatibility, the issue persists. Can anyone provide assistance?
I have checked existing solutions on this platform, but none have resolved the problem.