This same code was working perfect the other day, now its not working...
it appears it has something to do with access controls but I cant figure it out.
I found a similar issue on StackOverflow, however, I am not sure where to implement the db.createUser code
here is a similar StackOverflow questions: MongoDB - admin user not authorized
below is my code
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
(node:7206) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:7206) [MONGODB DRIVER] Warning: Top-level use of w, wtimeout, j, and fsync is deprecated. Use writeConcern instead.
(node:7206) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Connected to database!
/Users/juan/StudioProjects/a-n-e-m-project/node_modules/mongodb/lib/core/connection/pool.js:453
return handleOperationCallback(self, workItem.cb, new MongoError(responseDoc));
^
MongoError: (Unauthorized) not authorized on admin to execute command { find: "posts", filter: { }, projection: { }, lsid: { id: {4 [159 148 249 216 42 112 71 195 180 178 144 58 57 47 151 207]} }, $clusterTime: { clusterTime: {1671116335 2}, signature: { hash: {0 [201 157 29 0 111 232 144 103 241 214 206 247 184 152 135 214 250 188 25 170]}, keyId: 7127068168341684224.000000 } }, $db: "admin" }
at Connection.<anonymous> (/Users/juan/StudioProjects/a-n-e-m-project/node_modules/mongodb/lib/core/connection/pool.js:453:61)
at Connection.emit (node:events:513:28)
at processMessage (/Users/juan/StudioProjects/a-n-e-m-project/node_modules/mongodb/lib/core/connection/connection.js:456:10)
at TLSSocket.<anonymous> (/Users/juan/StudioProjects/a-n-e-m-project/node_modules/mongodb/lib/core/connection/connection.js:625:15)
at TLSSocket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
ok: 0,
code: 8000,
codeName: 'AtlasError'
}
[nodemon] app crashed - waiting for file changes before starting...
this is where the server side code is, aswell as all the code::
https://github.com/casas1010/mean-project/blob/main/backend/app.js
i have admin rights and the MongoDB should allowed access from any IP
here is the code making the connection::
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const postsRoutes = require("./routes/posts");
const app = express();
mongoose
.connect(
"mongodb srv://USERNAME:[email protected]/?retryWrites=true&w=majority"
)
.then(() => {
console.log("Connected to database!");
})
.catch(() => {
console.log("Connection failed!");
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
next();
});
app.use("/api/posts", postsRoutes);
module.exports = app;
CodePudding user response:
the problem is that you do not have an authorized user in mongodb atlas, check if you have the user authorized to use in that database, the database in which you need to access has an authorization. Take a look at accessing a new user from mongosh to access that db, or see if the username or password is correct. I hope it has served you, I will look to access more information if it goes wrong ;)
Edit: You will need to change the user "admin" to access this database
db.createUser({ user: "blogUser", pwd: "blogUserPwd", roles: [{ role: "dbAdmin", db: "blog" }, { role: "readWrite", db: "blog" } ]})
CodePudding user response:
i ended up downgrading the mongoose version and it worked fine