'What is wrong wrong in try block or model?
First error I got is: In status, I was getting 500, and also returning {} empty curly braces in postman. what is the error in the try block?
const router = require("express").Router();
const User = require("../models/User");
//Register
router.post("/register", async(req, res) => {
try {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password
});
const user = await newUser.save();
res.status(200).json(user);
} catch (err) {
res.status(500).json(err);
}
});
user model file
const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema({
username: {
type: String,
requried: true,
unique: true
},
email: {
type: String,
requried: true,
unique: true,
},
password: {
type: String,
required: true
},
profilePic: {
type: String,
default: ""
}
},
{ timestamps: true }
);
module.exports = mongoose.model("User", UserSchema);
after solving first error, second error I got is
{
"errors": {
"password": {
"name": "ValidatorError",
"message": "Path `password` is required.",
"properties": {
"message": "Path `password` is required.",
"type": "required",
"path": "password",
"value": ""
},
"kind": "required",
"path": "password",
"value": ""
}
},
"_message": "User validation failed",
"name": "ValidationError",
"message": "User validation failed: password: Path `password` is required."
}
I don't understand where my code went wrong. Can anyone please help me out!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
