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 // Create new user => POST api/users/create
const createUser = asyncHandler(async (req, res) => { const createUser = asyncHandler(async (req, res) => {
try { try {
const { username, const { username, password, email, isGoogleAccount } = req.body;
password,
email,
isGoogleAccount } = req.body;
const users = await user.create({ const newUser = await user.create({
username, username,
password, password,
email, email,
isGoogleAccount isGoogleAccount
}); });
res.status(200).json(users); // Respond with the newly created user
} catch(err) { res.status(201).json(newUser);
console.log(err) } 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, required: true,
select : false select : false
}, },
imgPath: {
type: String,
required: true
},
email: { email: {
type: String, type: String,
required: true required: true

View file

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