Reputation: 1266
I'm working on creating Auth API using Mongo and Nodejs. I have done the initial part. But when i hit the register API, it returns an empty object. Here's my scheme:
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
min: 6,
max: 255
},
email: {
type: String,
required: true,
max: 255,
min: 6
},
password: {
type: String,
required: true,
max: 1024,
min: 6
},
date: {
type: Date,
default: Date.now
}
});
const User = mongoose.model('User', userSchema);
module.exports = User;
And here's where the request is being sent:
const router = require('express').Router();
const User = require('../model/User');
router.post('/register', async (req, res) => {
const user = new User({
name: req.body.name,
email: req.body.email,
password: req.body.password
});
try{
const savedUser = await user.save();
res.send(savedUser);
}catch(err){
res.status(400).send(err);
}
})
module.exports = router;
Now the user.save
is not working for some reason. Not sure what i'm doing wrong. Thanks in advance.
Upvotes: 0
Views: 46
Reputation: 226
It automatically gets the _id when it creates an object from the User Scheme.So it would be pointless to reassign
try{
await user.save();
res.send(user);
}catch(err){
res.status(400).send(err);
}
Upvotes: 2