mirror of
https://gitlab.com/little-lines/backend.git
synced 2024-11-21 18:16:51 +00:00
create userHandler
This commit is contained in:
parent
f1f614d4e7
commit
cdd52920dc
4 changed files with 14 additions and 13 deletions
|
@ -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.' });
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -13,10 +13,6 @@ const userSchema = new mongoose.Schema({
|
|||
required: true,
|
||||
select : false
|
||||
},
|
||||
imgPath: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
required: true
|
||||
|
|
|
@ -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, () => {
|
||||
|
|
Loading…
Reference in a new issue