seungmi

선별된 목록을 배열에 저장

기존의 temp.json파일에 음식 목록을 선별하여 저장하던 방식에서
선별된 음식 목록을 배열에 저장하는 방식으로 변환
(write함수와 read함수 제거)
Showing 1 changed file with 5 additions and 33 deletions
......@@ -66,9 +66,6 @@ app.post('/food/recommendation', function(req, res) {
var kinds = [];
kinds.push(req.body.kinds);
}
// var len_shape=shape.length;
// var len_kinds=kinds.length;
var set1 = new Set();
var set2 = new Set();
var set3 = new Set();
......@@ -93,54 +90,29 @@ app.post('/food/recommendation', function(req, res) {
} else {
set2.add(foods);
}
if (!price || (price&&users[foods]['price'] <= Number(price)+5000 && users[foods]['price'] >= Number(price)-5000)) {
if (!price || (price && users[foods]['price'] <= Number(price) + 5000 && users[foods]['price'] >= Number(price) - 5000)) {
set3.add(foods);
console.log(users[foods]['price']);
}
}
console.log(shape);
console.log(kinds);
console.log(set1);
console.log(set2);
console.log(set3);
// console.log('shapeleng:' + shape.length);
// console.log('kinds leng:' + kinds.length);
callback(set1, set2, set3);
}
add(users, price, shape, kinds, function(set1, set2, set3) {
let difference1 = new Set([...set1].filter(i => set2.has(i)));
let difference2 = new Set([...difference1].filter(i => set3.has(i)));
var list = {
food: [...difference2]
};
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) {
console.log((err));
res.status(500).send('Internal Server Error');
} else {
var food = JSON.parse(data)['food'];
var food = [...difference2];
var length = food.length;
console.log('food 목록: '+food);
console.log('food 갯수: '+length);
var rand = Math.floor(Math.random() * length); //(Math.random() * (max - min)) + min
var food_value = food[rand];
console.log(rand);
console.log('랜덤 food 번호: '+rand);
res.render('print.ejs', {
title: 'Recommendation',
description: 'We recommend this...',
randvalue: food_value
}); //rander closed
}
}); //readFile closed
}
}); //writeFile closed
}); //add closed
}
}); //readFile closed
}); //post closed
......