0

I was trying to learn MongoDB CRUD operations along with node express js

when i try to go 'http://localhost:5000/service' its throwing the following error :

 D:\Projects_WebDev\BackEnd\server-genius-car-service\node_modules\mongodb\lib\cmap\connection.js:207
                    callback(new error_1.MongoServerError(document));
                             ^

MongoServerError: user is not allowed to do action [find] on [genius-car.service]
    at Connection.onMessage (D:\Projects_WebDev\BackEnd\server-genius-car-service\node_modules\mongodb\lib\cmap\connection.js:207:30)
    at MessageStream.<anonymous> (D:\Projects_WebDev\BackEnd\server-genius-car-service\node_modules\mongodb\lib\cmap\connection.js:60:60)
    at MessageStream.emit (node:events:527:28)
    at processIncomingData (D:\Projects_WebDev\BackEnd\server-genius-car-service\node_modules\mongodb\lib\cmap\message_stream.js:132:20)  
    at MessageStream._write (D:\Projects_WebDev\BackEnd\server-genius-car-service\node_modules\mongodb\lib\cmap\message_stream.js:33:9)   
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at MessageStream.Writable.write (node:internal/streams/writable:334:10)
    at TLSSocket.ondata (node:internal/streams/readable:754:22)
    at TLSSocket.emit (node:events:527:28) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError',
  [Symbol(errorLabels)]: Set(0) {}
}
[nodemon] app crashed - waiting for file changes before starting...

Code : `

async function run() {
    try {
        await client.connect();
        const serviceCollection = client.db('genius-car').collection('service');

        app.get('/service', async (req, res) => {
            const query = {};
            const cursor = serviceCollection.find(query);
            const services = await cursor.toArray();
            res.send(services)
        });


    } finally {
        //
    }

}
run().catch(console.dir);`
  • There are some guys meet the same problem, check out the question from other users and figure the way to fix: https://stackoverflow.com/questions/46649390/mongoerror-user-is-not-allowed-to-do-action – Nguyen Ho Sep 01 '22 at 03:54
  • tried but cant make the way to fix it out – Monzur Adler Sep 01 '22 at 11:48

2 Answers2

1

Follow the steps to fix this problem:

  1. Sign-In your MongoDB Atlas account
  2. Clicked on Database Access > Database Users > EDIT actions for certain user > Specific Privileges > Add Specific Privilege > readWriteAnyDatabase (Select Role) > Update User

After following these steps, you will need to run your server again and reload the 'http://localhost:5000/service' page. If you follow all the steps correctly, then it will works.

0

If you get the error:

user is not allowed to do action [insert]

In your MongoDB account, navigate to Database Access then click on the edit button for the user trying to access, navigate to special privileges and click on "Add special Previledge" select the role option and change the role to readWriteAnyDatabase

Tyler2P
  • 2,324
  • 26
  • 22
  • 31