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 );
......
...@@ -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++) {
...@@ -206,8 +237,19 @@ app.post('/api/pick', (req, res) => { ...@@ -206,8 +237,19 @@ app.post('/api/pick', (req, res) => {
206 code: 401, 237 code: 401,
207 message: 'card exist' 238 message: 'card exist'
208 }); 239 });
209 - } else { 240 + }
210 - let flag = true; 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;
211 let user_picks = rows[0].pick.split(','); 253 let user_picks = rows[0].pick.split(',');
212 user_picks.pop(); 254 user_picks.pop();
213 for(let i=0; i<user_picks.length; i++) { 255 for(let i=0; i<user_picks.length; i++) {
...@@ -224,13 +266,14 @@ app.post('/api/pick', (req, res) => { ...@@ -224,13 +266,14 @@ app.post('/api/pick', (req, res) => {
224 message: 'insertion success', 266 message: 'insertion success',
225 }); 267 });
226 }) 268 })
227 - } else { 269 + }
270 + else {
228 return res.status(401).json({ 271 return res.status(401).json({
229 code: 401, 272 code: 401,
230 message: 'card exist' 273 message: 'card exist'
231 }); 274 });
232 } 275 }
233 - 276 + }
234 } 277 }
235 }) 278 })
236 }); 279 });
...@@ -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();
......