-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.js
executable file
·44 lines (36 loc) · 1.24 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import User from '../models/User.js'
// @description Post one user
// @route POST /api/v1/users
// @access Public
const postUser = async (req, res, next) => {
try {
// create pins with mongoose and save it in mongodb
const user = await User.create(req.body); // userId needs to match to schema
// send back a response
return res.status(200).json({
success: true,
data: user
})
} catch (error) {
if(error.code === 11000){
return res.status(400).json({ error: 'DUPLICATE_KEY' }) // 400 error is a USER error meaning that the user sent something that shouldnt have been sent
}
res.status(500).json({ error: error})
}
};
const updateUser = async (req, res, next) => {
try {
const user = await User.updateOne(
{ "userId" : req.body.userId},
{ $set: { "location" : {"coordinates": [req.body.location.coordinates[0],req.body.location.coordinates[1]]} } }
);
return res.status(200).json({
success: true,
data: user
})
} catch (error) {
console.log(error)
res.status(500).json({ error: error})
}
}
export { postUser, updateUser };