swa07016

node mysql connection error 해결

...@@ -24,6 +24,7 @@ const MealCard = (props) => { ...@@ -24,6 +24,7 @@ const MealCard = (props) => {
24 headers: { 24 headers: {
25 'Content-Type': 'application/json', 25 'Content-Type': 'application/json',
26 'authorization': user 26 'authorization': user
27 +
27 } 28 }
28 }).then(response => response.json()) 29 }).then(response => response.json())
29 .then(result => { 30 .then(result => {
......
...@@ -14,6 +14,7 @@ const UserCards = (props) => { ...@@ -14,6 +14,7 @@ const UserCards = (props) => {
14 headers: { 14 headers: {
15 'Content-Type': 'application/json', 15 'Content-Type': 'application/json',
16 'authorization': user 16 'authorization': user
17 +
17 } 18 }
18 }).then(response => response.json()) 19 }).then(response => response.json())
19 .then( result => { 20 .then( result => {
...@@ -47,7 +48,7 @@ const UserCards = (props) => { ...@@ -47,7 +48,7 @@ const UserCards = (props) => {
47 <br/> 48 <br/>
48 <Container style={{'paddingTop':'1.2rem'}}> 49 <Container style={{'paddingTop':'1.2rem'}}>
49 <Row xs="2" sm="2" md="4"> 50 <Row xs="2" sm="2" md="4">
50 - {picks.map((data, index) => 51 + {picks && picks.map((data, index) =>
51 <Col key = {index}> 52 <Col key = {index}>
52 <PickedCard 53 <PickedCard
53 key = {index} 54 key = {index}
......
...@@ -26,7 +26,7 @@ const LandingPage = (props) => { ...@@ -26,7 +26,7 @@ const LandingPage = (props) => {
26 useEffect(() => { 26 useEffect(() => {
27 const fetchData = async () => { 27 const fetchData = async () => {
28 const result = await axios( 28 const result = await axios(
29 - '/api/datas', 29 + '/api/datas'
30 ); 30 );
31 setDatas(result.data); 31 setDatas(result.data);
32 }; 32 };
......
...@@ -27,8 +27,7 @@ const MenuPage = (props) => { ...@@ -27,8 +27,7 @@ const MenuPage = (props) => {
27 useEffect(() => { 27 useEffect(() => {
28 const fetchData = async () => { 28 const fetchData = async () => {
29 const result = await axios( 29 const result = await axios(
30 - '/api/datas', 30 + '/api/datas'
31 - // localhost로 바꾸기
32 ); 31 );
33 setDatas(result.data); 32 setDatas(result.data);
34 setIsLoading(true); 33 setIsLoading(true);
...@@ -169,7 +168,7 @@ const MenuPage = (props) => { ...@@ -169,7 +168,7 @@ const MenuPage = (props) => {
169 {filteredDatas.map((data, index) => 168 {filteredDatas.map((data, index) =>
170 <Col> 169 <Col>
171 <MealCard 170 <MealCard
172 - key = {index} 171 + key = {data.id}
173 id = {data.id} 172 id = {data.id}
174 name = {data.name} 173 name = {data.name}
175 address = {data.address} 174 address = {data.address}
......
...@@ -14,6 +14,7 @@ const MypickPage = () => { ...@@ -14,6 +14,7 @@ const MypickPage = () => {
14 headers: { 14 headers: {
15 'Content-Type': 'application/json', 15 'Content-Type': 'application/json',
16 'authorization': user 16 'authorization': user
17 +
17 } 18 }
18 }).then(response => response.json()) 19 }).then(response => response.json())
19 .then(result => { 20 .then(result => {
......
...@@ -13,8 +13,10 @@ const SigninPage = (props) => { ...@@ -13,8 +13,10 @@ const SigninPage = (props) => {
13 method: 'POST', 13 method: 'POST',
14 headers: { 14 headers: {
15 'Content-Type': 'application/json' 15 'Content-Type': 'application/json'
16 +
16 }, 17 },
17 - body: JSON.stringify(user) 18 + body: JSON.stringify(user),
19 +
18 }).then(response => response.json()) 20 }).then(response => response.json())
19 } 21 }
20 22
......
...@@ -30,11 +30,12 @@ const SigninPage = (props) => { ...@@ -30,11 +30,12 @@ const SigninPage = (props) => {
30 body: JSON.stringify(signupInfo), 30 body: JSON.stringify(signupInfo),
31 headers: { 31 headers: {
32 "Content-Type": "application/json" 32 "Content-Type": "application/json"
33 +
33 } 34 }
34 }; 35 };
35 36
36 if( username && password ) { 37 if( username && password ) {
37 - fetch("http://localhost:3000/api/signup", signup_info) 38 + fetch("/api/signup", signup_info)
38 .then(response => response.json()) 39 .then(response => response.json())
39 .then(json => { 40 .then(json => {
40 if(json.message === 'success') { 41 if(json.message === 'success') {
......
...@@ -4,7 +4,7 @@ module.exports = function(app) { ...@@ -4,7 +4,7 @@ module.exports = function(app) {
4 app.use( 4 app.use(
5 '/api', 5 '/api',
6 createProxyMiddleware({ 6 createProxyMiddleware({
7 - target: 'http://localhost:5000', 7 + target: 'http://localhost:5000/',
8 changeOrigin: true, 8 changeOrigin: true,
9 }) 9 })
10 ); 10 );
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
15 "author": "SeongHoon", 15 "author": "SeongHoon",
16 "license": "ISC", 16 "license": "ISC",
17 "dependencies": { 17 "dependencies": {
18 - "bcrypt": "^4.0.1", 18 + "bcrypt": "^4.0.1",
19 "body-parser": "^1.19.0", 19 "body-parser": "^1.19.0",
20 "concurrently": "^5.2.0", 20 "concurrently": "^5.2.0",
21 "express": "^4.17.1", 21 "express": "^4.17.1",
...@@ -24,4 +24,4 @@ ...@@ -24,4 +24,4 @@
24 "jwt-decode": "^2.2.0", 24 "jwt-decode": "^2.2.0",
25 "mysql": "^2.18.1" 25 "mysql": "^2.18.1"
26 } 26 }
27 -} 27 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -29,7 +29,25 @@ const connection = mysql.createConnection({ ...@@ -29,7 +29,25 @@ const connection = mysql.createConnection({
29 database: conf.database, 29 database: conf.database,
30 }); 30 });
31 31
32 -connection.connect(); 32 +function handleDisconnect() {
33 + connection.connect(function(err) {
34 + if(err) {
35 + console.log('error when connecting to connection:', err);
36 + setTimeout(handleDisconnect, 2000);
37 + }
38 + });
39 +
40 + connection.on('error', function(err) {
41 + console.log('connection error', err);
42 + if(err.code === 'PROTOCOL_CONNECTION_LOST') {
43 + return handleDisconnect();
44 + } else {
45 + throw err;
46 + }
47 + });
48 +}
49 +
50 +handleDisconnect();
33 51
34 app.use(bodyParser.json()); 52 app.use(bodyParser.json());
35 app.use(bodyParser.urlencoded({ extended: true })); 53 app.use(bodyParser.urlencoded({ extended: true }));
...@@ -179,8 +197,21 @@ app.get("/api/mypicks", (req, res) => { ...@@ -179,8 +197,21 @@ app.get("/api/mypicks", (req, res) => {
179 let temp = iconv.decode(dataBuffer, "EUC-KR"); 197 let temp = iconv.decode(dataBuffer, "EUC-KR");
180 198
181 connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds) => { 199 connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds) => {
200 + if(rows.length === 0) {
201 + return res.status(401).json({
202 + code: 401,
203 + message: 'card 0'
204 + });
205 + }
206 + if(rows[0].pick === null) {
207 + return res.status(401).json({
208 + code: 401,
209 + message: 'card 0'
210 + });
211 + }
182 212
183 let user_picks = rows[0].pick.split(','); 213 let user_picks = rows[0].pick.split(',');
214 + console.log(rows[0].pick);
184 temp = JSON.parse(temp); 215 temp = JSON.parse(temp);
185 user_picks.pop(); 216 user_picks.pop();
186 for(let i=0; i<user_picks.length; i++) { 217 for(let i=0; i<user_picks.length; i++) {
...@@ -196,43 +227,55 @@ app.get("/api/mypicks", (req, res) => { ...@@ -196,43 +227,55 @@ app.get("/api/mypicks", (req, res) => {
196 227
197 app.post('/api/pick', (req, res) => { 228 app.post('/api/pick', (req, res) => {
198 229
199 - const user = jwt_decode(req.headers.authorization); 230 + const user = jwt_decode(req.headers.authorization);
200 - const username = user.name; 231 + const username = user.name;
201 - const cardid = req.body.cardid; 232 + const cardid = req.body.cardid;
202 233
203 - connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> { 234 + connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> {
204 - if(rows.length === 0) { 235 + if(rows.length === 0) {
236 + return res.status(401).json({
237 + code: 401,
238 + message: 'card exist'
239 + });
240 + }
241 + else {
242 + if(rows[0].pick === null || rows[0].pick === '') {
243 + const newPick = cardid.toString() + ',';
244 + connection.query(`UPDATE USER SET pick='${newPick}' WHERE NAME='${username}';`, (err, rows, fields) => {
245 + return res.status(200).json({
246 + code: 200,
247 + message: 'insertion success',
248 + });
249 + })
250 + }
251 + else {
252 + var flag = true;
253 + let user_picks = rows[0].pick.split(',');
254 + user_picks.pop();
255 + for(let i=0; i<user_picks.length; i++) {
256 + user_picks[i] = parseInt(user_picks[i]);
257 + if(user_picks[i] == cardid) {
258 + flag = false;
259 + }
260 + }
261 + if(flag) {
262 + const newPick = rows[0].pick + cardid.toString() + ',';
263 + connection.query(`UPDATE USER SET pick='${newPick}' WHERE NAME='${username}';`, (err, rows, fields) => {
264 + return res.status(200).json({
265 + code: 200,
266 + message: 'insertion success',
267 + });
268 + })
269 + }
270 + else {
205 return res.status(401).json({ 271 return res.status(401).json({
206 code: 401, 272 code: 401,
207 message: 'card exist' 273 message: 'card exist'
208 }); 274 });
209 - } else {
210 - let flag = true;
211 - let user_picks = rows[0].pick.split(',');
212 - user_picks.pop();
213 - for(let i=0; i<user_picks.length; i++) {
214 - user_picks[i] = parseInt(user_picks[i]);
215 - if(user_picks[i] == cardid) {
216 - flag = false;
217 - }
218 - }
219 - if(flag) {
220 - const newPick = rows[0].pick + cardid.toString() + ',';
221 - connection.query(`UPDATE USER SET pick='${newPick}' WHERE NAME='${username}';`, (err, rows, fields) => {
222 - return res.status(200).json({
223 - code: 200,
224 - message: 'insertion success',
225 - });
226 - })
227 - } else {
228 - return res.status(401).json({
229 - code: 401,
230 - message: 'card exist'
231 - });
232 - }
233 -
234 } 275 }
235 - }) 276 + }
277 + }
278 + })
236 }); 279 });
237 280
238 app.post('/api/delete', (req, res) => { 281 app.post('/api/delete', (req, res) => {
...@@ -248,6 +291,12 @@ app.post('/api/delete', (req, res) => { ...@@ -248,6 +291,12 @@ app.post('/api/delete', (req, res) => {
248 }); 291 });
249 } else { 292 } else {
250 let flag = false; 293 let flag = false;
294 + if(rows[0].pick === null) {
295 + return res.status(401).json({
296 + code: 401,
297 + message: 'card 0'
298 + });
299 + }
251 let user_picks = rows[0].pick.split(','); 300 let user_picks = rows[0].pick.split(',');
252 let newPick = ''; 301 let newPick = '';
253 user_picks.pop(); 302 user_picks.pop();
......