Skip to content

Commit 06ee8d7

Browse files
committedMar 11, 2020
updating boilerplate Mongo connection
1 parent e3cf6a5 commit 06ee8d7

File tree

7 files changed

+84
-271
lines changed

7 files changed

+84
-271
lines changed
 

‎UserSchema.js

-8
This file was deleted.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@testing-library/jest-dom": "^4.2.4",
88
"@testing-library/react": "^9.3.2",
99
"@testing-library/user-event": "^7.1.2",
10+
"cors": "^2.8.5",
1011
"dotenv": "^8.2.0",
1112
"express": "^4.17.1",
1213
"mongoose": "^5.8.1",

‎schemas/ExamSchema.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var mongoose = require('mongoose');
2+
3+
// example of an "exam schema"
4+
var exam = mongoose.Schema({
5+
questions: [
6+
{
7+
question_id: String,
8+
questions: [String],
9+
correct_answer: String
10+
}
11+
]
12+
});
13+
14+
module.exports = exam;

‎schemas/UserSchema.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var mongoose = require('mongoose');
2+
3+
var userSchema = mongoose.Schema({
4+
firstName: String,
5+
lastName: String,
6+
active_test: String,
7+
answers: [
8+
{
9+
question_id: String,
10+
answer: String
11+
}
12+
],
13+
exam_id: String
14+
});
15+
16+
module.exports = userSchema;

‎server.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const express = require('express');
22
const app = express();
33
const port = process.env.PORT || 5000;
4-
const UserSchema = require('./UserSchema');
4+
const UserSchema = require('./schemas/UserSchema');
5+
const cors = require('cors');
56

7+
app.use(cors());
68
// console.log that your server is up and running
79
app.listen(port, () => console.log(`Listening on port ${port}`));
810

@@ -11,15 +13,15 @@ app.get('/express_backend', (req, res) => {
1113
res.send({ express: 'YOUR EXPRESS BACKEND IS CONNECTED TO REACT' });
1214
});
1315

14-
app.get('/add_user/:firstName/:lastName', (req, res) => {
16+
app.post('/add_user/:firstName/:lastName', (req, res) => {
1517
const { firstName, lastName } = req.params;
1618
const User = mongoose.model('UserSchema', UserSchema, 'UserSchema');
1719
const userSchema = new User({ firstName, lastName });
18-
20+
console.log({ firstName, lastName, userSchema });
1921
// save model to database
2022
userSchema.save((err, userSchema) => {
2123
if (err) return res.send({ err }).status(400);
22-
console.log({ userSchema });
24+
console.log({ userSchema, err });
2325
res.send({ userSchema }).status(200);
2426
});
2527
});

‎src/components/Tables/NameTable.js

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import TableContainer from '@material-ui/core/TableContainer';
77
import TableHead from '@material-ui/core/TableHead';
88
import TableRow from '@material-ui/core/TableRow';
99
import Paper from '@material-ui/core/Paper';
10+
// import { response } from 'express';
11+
import Button from '@material-ui/core/Button';
1012

1113
const useStyles = makeStyles({
1214
table: {
@@ -28,6 +30,23 @@ const rows = [
2830

2931
export default function NameTable() {
3032
const classes = useStyles();
33+
const makeUser = async () => {
34+
try {
35+
const response = await fetch(
36+
'http://localhost:5000/add_user/jessica/bennett',
37+
{
38+
method: 'POST',
39+
body: JSON.stringify({ firstName: 'jessica', lastName: 'bennett' })
40+
}
41+
);
42+
const { data } = response;
43+
console.log({ response, data });
44+
return response;
45+
} catch (error) {
46+
console.log({ error });
47+
return error;
48+
}
49+
};
3150

3251
return (
3352
<TableContainer component={Paper}>
@@ -49,6 +68,9 @@ export default function NameTable() {
4968
))}
5069
</TableBody>
5170
</Table>
71+
<Button variant='contained' onClick={() => makeUser()}>
72+
work
73+
</Button>
5274
</TableContainer>
5375
);
5476
}

0 commit comments

Comments
 (0)