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_WebDevBackEndserver-genius-car-servicenode_modulesmongodblibcmapconnection.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_WebDevBackEndserver-genius-car-servicenode_modulesmongodblibcmapconnection.js:207:30)
at MessageStream.<anonymous> (D:Projects_WebDevBackEndserver-genius-car-servicenode_modulesmongodblibcmapconnection.js:60:60)
at MessageStream.emit (node:events:527:28)
at processIncomingData (D:Projects_WebDevBackEndserver-genius-car-servicenode_modulesmongodblibcmapmessage_stream.js:132:20)
at MessageStream._write (D:Projects_WebDevBackEndserver-genius-car-servicenode_modulesmongodblibcmapmessage_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);`
2
Answers
If you get the error:
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:
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.