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,39 +60,40 @@ app.post('/food/recommendation', function(req, res) { ...@@ -60,39 +60,40 @@ 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 - }
69 - });
70 -
71 - fs.readFile('data/temp.json', 'utf8', function(err, data) {
72 - if (err) {
73 - console.log((err));
74 - res.status(500).send('Internal Server Error');
75 } else { 70 } else {
76 - var food = JSON.parse(data)['food']; 71 +
77 - var length=food.length; 72 + fs.readFile('data/temp.json', 'utf8', function(err, data) {
78 - var rand = Math.floor(Math.random() * length); //(Math.random() * (max - min)) + min 73 + if (err) {
79 - for (var i = 0; i < length; i++) { 74 + console.log((err));
80 - if (rand == i) { 75 + res.status(500).send('Internal Server Error');
81 - var food_value = food[i]; 76 + } else {
82 - break; 77 + var food = JSON.parse(data)['food'];
83 - // $('<li>').text(foods).appendTo('#users'); 78 + var length = food.length;
79 + var rand = Math.floor(Math.random() * length); //(Math.random() * (max - min)) + min
80 + var food_value = food[rand];
81 + console.log(rand);
82 +
83 + res.render('print.ejs', {
84 + title: 'Recommendation',
85 + description: 'We recommend this...',
86 + randvalue: food_value
87 + });//rander closed
84 } 88 }
85 - } 89 + });//readFile closed
86 - res.render('print.ejs', {
87 - title: 'Recommendation',
88 - description: 'We recommend this...',
89 - randvalue: food_value
90 - });
91 } 90 }
92 - }); 91 + });//writeFile closed
92 +
93 } 93 }
94 - }); 94 + });//readFile closed
95 -}) 95 +});//post closed
96 +
96 97
97 98
98 app.listen(3000, function() { 99 app.listen(3000, function() {
......