seungmi

post 함수 버그 수정

post함수의 비동기로 인한 write, read 함수 에러 내용수정
......@@ -38,8 +38,8 @@ app.get(['/food', '/food/:id'], function(req, res) {
//사용자가 호출한 search page
app.post('/food/search', function(req, res) {
var title=req.body.title;
res.redirect("https://www.google.com/search?q="+title+"&oq="+title+"&aqs=chrome..69i57j0l3j69i60j69i61.3149j0j9&sourceid=chrome&ie=UTF-8")
var title = req.body.title;
res.redirect("https://www.google.com/search?q=" + title + "&oq=" + title + "&aqs=chrome..69i57j0l3j69i60j69i61.3149j0j9&sourceid=chrome&ie=UTF-8")
})
//사용자가 호출한 recommendation page
......@@ -60,13 +60,14 @@ app.post('/food/recommendation', function(req, res) {
if ((!price || users[foods]['price'] == price) && (!shape || users[foods]['shape'] == shape) && (!kinds || users[foods]['kinds'] == kinds)) {
list.food.push(foods);
}
// $('<li>').text(foods).appendTo('#users');
}
fs.writeFile('data/temp.json', JSON.stringify(list), "utf8", function(err) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
}
});
} else {
fs.readFile('data/temp.json', 'utf8', function(err, data) {
if (err) {
......@@ -74,25 +75,25 @@ app.post('/food/recommendation', function(req, res) {
res.status(500).send('Internal Server Error');
} else {
var food = JSON.parse(data)['food'];
var length=food.length;
var length = food.length;
var rand = Math.floor(Math.random() * length); //(Math.random() * (max - min)) + min
for (var i = 0; i < length; i++) {
if (rand == i) {
var food_value = food[i];
break;
// $('<li>').text(foods).appendTo('#users');
}
}
var food_value = food[rand];
console.log(rand);
res.render('print.ejs', {
title: 'Recommendation',
description: 'We recommend this...',
randvalue: food_value
});
});//rander closed
}
});
});//readFile closed
}
});
})
});//writeFile closed
}
});//readFile closed
});//post closed
app.listen(3000, function() {
......