seungmi

post 함수 버그 수정

post함수의 비동기로 인한 write, read 함수 에러 내용수정
...@@ -38,8 +38,8 @@ app.get(['/food', '/food/:id'], function(req, res) { ...@@ -38,8 +38,8 @@ app.get(['/food', '/food/:id'], function(req, res) {
38 38
39 //사용자가 호출한 search page 39 //사용자가 호출한 search page
40 app.post('/food/search', function(req, res) { 40 app.post('/food/search', function(req, res) {
41 - var title=req.body.title; 41 + var title = req.body.title;
42 - res.redirect("https://www.google.com/search?q="+title+"&oq="+title+"&aqs=chrome..69i57j0l3j69i60j69i61.3149j0j9&sourceid=chrome&ie=UTF-8") 42 + res.redirect("https://www.google.com/search?q=" + title + "&oq=" + title + "&aqs=chrome..69i57j0l3j69i60j69i61.3149j0j9&sourceid=chrome&ie=UTF-8")
43 }) 43 })
44 44
45 //사용자가 호출한 recommendation page 45 //사용자가 호출한 recommendation page
...@@ -60,13 +60,14 @@ app.post('/food/recommendation', function(req, res) { ...@@ -60,13 +60,14 @@ app.post('/food/recommendation', function(req, res) {
60 if ((!price || users[foods]['price'] == price) && (!shape || users[foods]['shape'] == shape) && (!kinds || users[foods]['kinds'] == kinds)) { 60 if ((!price || users[foods]['price'] == price) && (!shape || users[foods]['shape'] == shape) && (!kinds || users[foods]['kinds'] == kinds)) {
61 list.food.push(foods); 61 list.food.push(foods);
62 } 62 }
63 + // $('<li>').text(foods).appendTo('#users');
63 } 64 }
65 +
64 fs.writeFile('data/temp.json', JSON.stringify(list), "utf8", function(err) { 66 fs.writeFile('data/temp.json', JSON.stringify(list), "utf8", function(err) {
65 if (err) { 67 if (err) {
66 console.log(err); 68 console.log(err);
67 res.status(500).send('Internal Server Error'); 69 res.status(500).send('Internal Server Error');
68 - } 70 + } else {
69 - });
70 71
71 fs.readFile('data/temp.json', 'utf8', function(err, data) { 72 fs.readFile('data/temp.json', 'utf8', function(err, data) {
72 if (err) { 73 if (err) {
...@@ -74,25 +75,25 @@ app.post('/food/recommendation', function(req, res) { ...@@ -74,25 +75,25 @@ app.post('/food/recommendation', function(req, res) {
74 res.status(500).send('Internal Server Error'); 75 res.status(500).send('Internal Server Error');
75 } else { 76 } else {
76 var food = JSON.parse(data)['food']; 77 var food = JSON.parse(data)['food'];
77 - var length=food.length; 78 + var length = food.length;
78 var rand = Math.floor(Math.random() * length); //(Math.random() * (max - min)) + min 79 var rand = Math.floor(Math.random() * length); //(Math.random() * (max - min)) + min
79 - for (var i = 0; i < length; i++) { 80 + var food_value = food[rand];
80 - if (rand == i) { 81 + console.log(rand);
81 - var food_value = food[i]; 82 +
82 - break;
83 - // $('<li>').text(foods).appendTo('#users');
84 - }
85 - }
86 res.render('print.ejs', { 83 res.render('print.ejs', {
87 title: 'Recommendation', 84 title: 'Recommendation',
88 description: 'We recommend this...', 85 description: 'We recommend this...',
89 randvalue: food_value 86 randvalue: food_value
90 - }); 87 + });//rander closed
91 } 88 }
92 - }); 89 + });//readFile closed
93 } 90 }
94 - }); 91 + });//writeFile closed
95 -}) 92 +
93 + }
94 + });//readFile closed
95 +});//post closed
96 +
96 97
97 98
98 app.listen(3000, function() { 99 app.listen(3000, function() {
......