SHOW:
|
|
- or go back to the newest paste.
1 | router.post('/', (req, res, next) => { | |
2 | const requiredFields = ['username', 'password', 'firstName', 'lastName']; | |
3 | const missingField = requiredFields.find(field => !(field in req.body)); | |
4 | ||
5 | - | if (missingField) { |
5 | + | if (missingField) { |
6 | - | return res.status(422).json({ |
6 | + | return res.status(422).json({ |
7 | - | code: 422, |
7 | + | code: 422, |
8 | - | reason: 'ValidationError', |
8 | + | reason: 'ValidationError', |
9 | - | message: 'Missing field', |
9 | + | message: 'Missing field', |
10 | - | location: missingField |
10 | + | location: missingField |
11 | - | }); |
11 | + | |
12 | - | } |
12 | + | } |
13 | ||
14 | - | let { username, password, firstName, lastName } = req.body; |
14 | + | let { username, password, firstName, lastName } = req.body; |
15 | - | let userQuestions = []; |
15 | + | |
16 | return User.find({ username }) | |
17 | - | return User.find({username}) |
17 | + | .count() |
18 | - | .count() |
18 | + | .then(count => { |
19 | - | .then(count => { |
19 | + | if (count > 0) { |
20 | - | if (count > 0) { |
20 | + | return Promise.reject({ |
21 | - | return Promise.reject({ |
21 | + | code: 422, |
22 | - | code: 422, |
22 | + | reason: 'ValidationError', |
23 | - | reason: 'ValidationError', |
23 | + | message: 'Username already taken', |
24 | - | message: 'Username already taken', |
24 | + | location: 'username' |
25 | - | location: 'username' |
25 | + | }); |
26 | } | |
27 | - | } |
27 | + | |
28 | - | }) |
28 | + | .then(() => Question.find()) |
29 | - | .then(() => { |
29 | + | |
30 | - | Question.find() |
30 | + | let hash = User.hashPassword(password); |
31 | let userQuestions = questions.map((question, index) => { | |
32 | - | questions.forEach((question, index) => { |
32 | + | return { |
33 | - | let q = { |
33 | + | question: question.question, |
34 | - | question: question.question, |
34 | + | answer: question.answer, |
35 | - | answer: question.answer, |
35 | + | next: index === questions.length - 1 ? null : index + 1, |
36 | - | next: index === questions.length - 1 ? null : index + 1, |
36 | + | mValue: 1, |
37 | - | mValue: 1, |
37 | + | numCorrect: 0, |
38 | - | numCorrect: 0, |
38 | + | numAttempts: 0 |
39 | - | numAttempts: 0 |
39 | + | } |
40 | - | }; |
40 | + | }); |
41 | - | userQuestions.push(q); |
41 | + | |
42 | - | }); |
42 | + | console.log(userQuestions); |
43 | ||
44 | - | .catch(err => console.log(err)); |
44 | + | return User.create({ |
45 | - | return User.hashPassword(password); |
45 | + | questions: userQuestions, |
46 | - | }) |
46 | + | firstName, |
47 | - | .then(hash => { |
47 | + | lastName, |
48 | - | console.log(userQuestions); // WHY IS THIS EMPTY?!?!?!?! |
48 | + | username, |
49 | - | return User.create({ |
49 | + | password: hash |
50 | - | questions: userQuestions, |
50 | + | }); |
51 | - | firstName, |
51 | + | |
52 | - | lastName, |
52 | + | .then(user => res.status(201).json(user)) |
53 | - | username, |
53 | + | .catch(err => { |
54 | - | password: hash |
54 | + | if (err.reason === 'ValidationError') { |
55 | - | }); |
55 | + | return res.status(err.code).json(err); |
56 | - | }) |
56 | + | } |
57 | - | .then(user => res.status(201).json(user)) |
57 | + | console.log(err); |
58 | - | .catch(err => { |
58 | + | res.status(500).json({ code: 500, message: err }); |
59 | - | if (err.reason === 'ValidationError') { |
59 | + | |
60 | - | return res.status(err.code).json(err); |
60 | + |