ディレクトリ構成
ソースコード
js
1// App.js 2 3import {BrowserRouter as Router, Route, Switch} from "react-router-dom"; 4import Home from "./pages/Home"; 5import Login from "./pages/Login"; 6import Signup from "./pages/Signup"; 7import TimeRecord from "./pages/TimeRecord"; 8 9function App() { 10 return ( 11 <div className="App"> 12 <Router> 13 <Switch> 14 <Route path="/" exact component={Home} /> 15 <Route path="/login" exact component={Login} /> 16 <Route path="/signup" exact component={Signup} /> 17 <Route path="/timeRecord" exact component={TimeRecord} /> 18 </Switch> 19 </Router> 20 </div> 21 ); 22} 23 24export default App; 25
js
1// Signup.js 2 3import styled from "styled-components"; 4import {Formik, Form, Field, ErrorMessage} from "formik"; 5import axios from "axios"; 6import * as Yup from "yup"; 7 8// styled-components → 9const Container = styled.div` 10 display: flex; 11 justify-content: center; 12 align-items: center; 13 width: 100vw; 14 height: 100vh; 15`; 16 17const Box = styled.div` 18 width: 300px; 19 border: 1px solid black; 20 border-radius: 10px; 21 padding: 15px 50px; 22 box-sizing: border-box; 23`; 24 25const Kiroku = styled.h2` 26 margin: 0; 27 font-size: 20px; 28`; 29 30const Title = styled.h2` 31 margin: 5px 0 40px 0; 32`; 33 34const InpBox = styled.div` 35 margin-bottom: 35px; 36`; 37 38const Label = styled.label` 39 display: block; 40`; 41 42const Button = styled.button` 43 display: block; 44 background-color: black; 45 color: white; 46 width: 160px; 47 text-align: center; 48 text-decoration: none; 49 margin-bottom: 50px; 50 `; 51// ← styled-components 52 53 54const Signup = () => { 55 56 const initialValues = { 57 username: "", 58 password: "" 59 }; 60 61 const validationSchema = Yup.object().shape({ 62 username: Yup.string().min(1).max(15).required(), 63 password: Yup.string().min(8).max(20).required(), 64 }); 65 66 const onSubmit = (data) => { 67 axios.post("http://localhost:3001/auth", data).then(() => { 68 console.log("送信されました"); // ① 69 console.log(data); 70 }).catch((err) => { 71 console.log("err:", err); 72 }); 73 } 74 75 return ( 76 <Container> 77 <Box> 78 <Kiroku>Kiroku</Kiroku> 79 <Title>サインアップ</Title> 80 <Formik 81 initialValues={initialValues} 82 onSubmit={onSubmit} 83 validationSchema={validationSchema} 84 > 85 <Form> 86 <InpBox> 87 <Label>ユーザ名</Label> 88 <Field 89 type="text" 90 name="username" 91 placeholder="1〜15字で登録してください" 92 /> 93 <ErrorMessage name="username" component="span" /> 94 </InpBox> 95 <InpBox> 96 <Label>ユーザ名</Label> 97 <Field 98 type="password" 99 name="password" 100 placeholder="8〜15字で登録してください" 101 /> 102 <ErrorMessage name="password" component="span" /> 103 </InpBox> 104 <Button type="submit">サインアップ</Button> 105 </Form> 106 </Formik> 107 </Box> 108 </Container> 109 ); 110} 111 112export default Signup;
js
1// Users.js 2 3const express = require("express"); 4const router = express.Router(); 5const bcrypt = require("bcrypt"); 6const db = require("../connection"); 7 8router.post("/", (req, res) => { 9 const {username, password} = req.body; 10 const sqlSelect = `SELECT * FROM Users WHERE username='${username}'`; 11 db.query(sqlSelect, (err_s, result_s) => { 12 if (result_s.length === 0) { 13 const sqlInsert = "INSERT INTO Users (username, password) VALUES (?, ?)"; 14 bcrypt.hash(password, 10).then((hash) => { 15 db.query(sqlInsert, [username, hash], (err_i, result_i) => { 16 if(err_i) console.log(err_i); 17 }); 18 }); 19 } else { 20 console.log("このユーザー名は使用できません"); 21 } 22 }); 23}); 24 25 26 27module.exports = router;
js
1// server/index.js 2 3const express = require("express"); 4const app = express(); 5const cors = require("cors"); 6 7app.use(express.json()); 8app.use(cors()); 9 10// Routers 11const usersRouter = require("./routes/Users"); 12app.use("/auth", usersRouter); 13 14app.listen(3001, () => { 15 console.log("Server running on port 3001"); 16}); 17
概要
Signup.jsでユーザー名とパスワードを入力した値をaxiosで送信し、Users.jsでデータベースに保存するという流れになっています。
解決したいこと
Signup.jsの①部分のconsole.log("送信されました"); console.log(data);
が表示されません。
どのようにすれば表示できるのでしょうか?
あなたの回答
tips
プレビュー