I am extremely new to MongoDB and am just now pushing more and more into fullstack development (mostly front end historically). I’m trying to build a simple authentication end point, and everything seems perfectly fine and error free (connection to DB is successful), until I try to create a new user in postman and it seems like no matter what I do, I get the following error:
{
"message": "Error creating user",
"error": {
"ok": 0,
"code": 8000,
"codeName": "AtlasError",
"name": "MongoError"
}
}
The issue I’m having is this error doesn’t seem to be telling me much and my attempts at searching on it (google, mongo docs) have not given me much to go on. I’m not necessarily looking to be handed the answer, but it would be nice if I could be pointed in the right direction because at this point I’m at a complete loss and have no idea where to even start looking. I’m fairly certain the issue isn’t in code but in my cluster setup, mostly because that’s the area of this that is by far most foreign to me, but I’m not really sure where to even start looking on that end of things. It seems to be hitting the catch in the user.save() block of the register endpoint in app.js, but I’m not sure if that implies an issue with the code, or an issue with the setup in mongo. Below are some potentially relevant code snippets:
.env file (password intentionally obscured):
DB_URL=mongodb+srv://emc_admin-prime:[PASSWORD]@cluster1.9ifxogd.mongodb.net/?retryWrites=true&w=majority
dbConnect file:
const mongoose = require("mongoose");
require('dotenv').config()
async function dbConnect() {
mongoose
.connect(
process.env.DB_URL,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => {
console.log("Successfully connected to MongoDB Atlas!");
})
.catch((error) => {
console.log("Unable to connect to MongoDB Atlas!");
console.error(error);
});
}
module.exports = dbConnect;
userModel file:
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
email: {
type: String,
required: [true, "Please provide an Email!"],
unique: [true, "Email Exist"],
},
password: {
type: String,
required: [true, "Please provide a password!"],
unique: false,
},
});
module.exports = mongoose.model.Users || mongoose.model("Users", UserSchema);
app.js file:
const express = require("express");
const app = express();
const bcrypt = require('bcrypt');
const bodyParser = require('body-parser');
const dbConnect = require('./db/dbConnect');
const User = require('./db/userModel');
dbConnect();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (request, response, next) => {
response.json({ message: 'Hey! This is your server response!' });
next();
});
app.post('/register', (request, response) => {
bcrypt.hash(request.body.password, 10)
.then((hashedPassword) => {
const user = new User({
email: request.body.email,
password: hashedPassword,
});
user
.save()
.then((result) => {
response.status(201).send({
message: 'User created successfully',
result,
});
})
.catch((error) => {
response.status(500).send({
message: 'Error creating user',
error,
});
});
})
.catch((error) => {
response.status(500).send({
message: 'Password was not hashed successfully',
error,
});
});
});
module.exports = app;