create userHandler

This commit is contained in:
NekoVari 2023-09-27 00:15:52 +07:00
parent f1f614d4e7
commit cdd52920dc
4 changed files with 14 additions and 13 deletions

View file

@ -11,21 +11,26 @@ const getAllUser = asyncHandler(async (req, res) => {
// Create new user => POST api/users/create
const createUser = asyncHandler(async (req, res) => {
try {
const { username,
password,
email,
isGoogleAccount } = req.body;
const { username, password, email, isGoogleAccount } = req.body;
const users = await user.create({
const newUser = await user.create({
username,
password,
email,
isGoogleAccount
});
res.status(200).json(users);
} catch(err) {
console.log(err)
// Respond with the newly created user
res.status(201).json(newUser);
} catch (err) {
// Handle validation errors
if (err.name === 'ValidationError') {
console.error(err);
return res.status(400).json({ error: err.message });
}
console.error(err);
res.status(500).json({ error: 'An error occurred while creating the user.' });
}
});

View file

@ -13,10 +13,6 @@ const userSchema = new mongoose.Schema({
required: true,
select : false
},
imgPath: {
type: String,
required: true
},
email: {
type: String,
required: true

View file

@ -6,7 +6,7 @@ const connectDb = require('./config/dbConnection');
app.use(express.json());
app.use(cors());
app.use('/api/users', require('./routes/user'));
app.use('/api/users', require('./routes/userRoute'));
app.listen(process.env.PORT, () => {