I am working through the MongoDB & NPM tutorials but have issues with my permissions when running CRUD functions. I receive this error:
**MongoServerError: user is not allowed to do action [insert, find] on [db.collection]
code: 8000,
codeName: ‘AtlasError’,
I’ve tried to change read/write permissions for my org and project user but it does not seem to work. I successfully connected to the DB per my terminal logs.
Can anyone help me out, please? Additionally, if I am sending my code to someone and they need to permit themselves, how can they do that on their side without my help?
Here is my code:
export async function insertNewBook(collection) {
const newBook = {
id: Math.floor(Math.random() * 100) * 2,
title: "Moneyball",
author: "Michael Lewis",
publicationYear: 2003,
};
await collection.insertOne(newBook);
}
export async function executeCRUDLogic() {
const uri = process.env.DB_URI;
try {
client = await connectToCluster(uri);
const db = client.db("Books_db");
const collection = db.collection("Books");
// POST a newBook to the collection
console.log("adding new book")
await insertNewBook(collection)
console.log("book added to collection")
// read operation - return all Books
function returnAllBooks(collection, id, title, author, publicationYear) {
return collection.find({ id, title, author, publicationYear }).toArray();
}
console.log(await returnAllBooks(collection, 3, "1984", "George Orwell", 1949))
}
catch(error) {
console.error("Error inserting book: ", error)
}
finally {
await client.close();
}
}