'Why does this Express API require a username
I had this API working for ages, but suddenly it has decided to die on me! I do not know why. I've tried to redact username, as I don't think it's required for my process.
I do not have username referenced or even apart of my schema at all. See below:
const UserSchema = new mongoose.Schema(
{
fullname:{type:String, required: true, default: "none"},
email:{type:String, required: true, unique: true},
position:{type:String, required: true, default: "none"},
password:{type:String, required: true},
company: {type:String, default: "none"},
recruiter: {type: Boolean}, //Canidate or Recruiter
isAdmin:{
type: Boolean,
default: false,
},
},
{timestamps: true }
);
module.exports = mongoose.model("User", UserSchema)
With this, I am using the following route.
router.post("/register", async (req, res) => {
const newUser = new User({
fullname: req.body.fullname,
email: req.body.email,
position: req.body.position,
// username: req.body.fullname,
password: CryptoJS.AES.encrypt(
req.body.password,
process.env.PASS_SEC
).toString(),
});
try {
const savedUser = await newUser.save();
res.status(201).json(savedUser);
} catch (err) {
res.status(500).json(err);
}
});
If I then try to attempt to post this without the username there, despite it never being referenced I get this.
E.g, trial input:
{
"fullname": "D D",
"email": "[email protected]",
"position": "Data Analyst",
"password": "nanana"
}
I then recieve this back from my post req. What the bloody hell am I doing wrong!
{
"index": 0,
"code": 11000,
"keyPattern": {
"username": 1
},
"keyValue": {
"username": null
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
