I am new to IT, I started learning React and JS through video courses. When creating a site for a video course, I encountered a number of errors: when I try to send a request wrapped in a try catch, I always get an error. I don't understand why this is happening, please help.
index.js
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const authRoute = require("./routes/auth");
dotenv.config();
mongoose
.connect(process.env.URL_MONGO, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Successfully connected"))
.catch((err) => {
console.error(err);
});
app.use(express.json());
app.use("/api/auth", authRoute);
app.listen(8800, () => {
console.log("Server is running!");
});
User.js
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema(
{
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
profilePic: { type: String, defaut: "" },
isAdmin: { type: Boolean, default: false },
},
{ timestamps: true }
);
module.exports = mongoose.model("User", UserSchema);
auth.js
const router = require("express").Router();
const User = require("../models/User");
router.post("/registration", async (req, res) => {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password,
})
try {
const user = await newUser.save();
res.status(201).json(user);
} catch (err) {
res.status(500).json(err);
}
});
module.exports = router;
For what I see on your img, you're doing a Netflix clone as I'm doing.
I can see that your request on postman is:
{
"username" = "eeqeqeqw",
"email": "nova@gmail.com",
"password":"2221"
}
So when working with objects, the values are asigned with ":" instead of "=" symbol.
Try to do the same request this way:
{
"username": "eeqeqeqw",
"email": "nova@gmail.com",
"password":"2221"
}