简体   繁体   中英

MongoServerError: user is not allowed to do action [find] on [genius-car.service]

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);`

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM