Yaswanth56 commited on
Commit
5d1dc3d
·
verified ·
1 Parent(s): d1b90c5

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +13 -2
static/script.js CHANGED
@@ -93,6 +93,7 @@ function handleResponse(userInput) {
93
  }
94
 
95
  function fetchIngredients(foodPreference) {
 
96
  fetch('/get_ingredients', {
97
  method: 'POST',
98
  headers: {
@@ -100,18 +101,28 @@ function fetchIngredients(foodPreference) {
100
  },
101
  body: JSON.stringify({ dietary_preference: foodPreference })
102
  })
103
- .then(response => response.json())
 
 
 
 
 
 
104
  .then(data => {
 
105
  if (data.error) {
106
  addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
 
 
107
  } else {
108
- const ingredients = data.ingredients || [];
109
  addMessage('bot', 'Please select ingredients:');
110
  displayIngredientsList(ingredients);
111
  displaySelectedIngredients();
112
  }
113
  })
114
  .catch(error => {
 
115
  addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
116
  });
117
  }
 
93
  }
94
 
95
  function fetchIngredients(foodPreference) {
96
+ console.log(`Fetching ingredients for dietary preference: ${foodPreference}`);
97
  fetch('/get_ingredients', {
98
  method: 'POST',
99
  headers: {
 
101
  },
102
  body: JSON.stringify({ dietary_preference: foodPreference })
103
  })
104
+ .then(response => {
105
+ console.log(`Response status: ${response.status}`);
106
+ if (!response.ok) {
107
+ throw new Error(`HTTP error! Status: ${response.status}`);
108
+ }
109
+ return response.json();
110
+ })
111
  .then(data => {
112
+ console.log('Response data:', data);
113
  if (data.error) {
114
  addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
115
+ } else if (!data.ingredients || data.ingredients.length === 0) {
116
+ addMessage('bot', 'No ingredients found for this selection. Please try another option.');
117
  } else {
118
+ const ingredients = data.ingredients;
119
  addMessage('bot', 'Please select ingredients:');
120
  displayIngredientsList(ingredients);
121
  displaySelectedIngredients();
122
  }
123
  })
124
  .catch(error => {
125
+ console.error('Fetch error:', error);
126
  addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
127
  });
128
  }