Skip to content

Commit

Permalink
process responses from api
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkaterina committed Feb 29, 2024
1 parent d25e773 commit 96d6fe8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions functions/api-controllers/foodDatabaseAPI-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const autocomplete = async (req, res, next) => {
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(response.statusText);
}
const result = await response.json();
res.status(200).json(result);
} catch (err) {
Expand All @@ -41,6 +44,9 @@ const parseQuery = async (req, res, next) => {
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(response.statusText);
}
const result = await response.json();
res.status(200).json(result);
} catch (err) {
Expand Down Expand Up @@ -73,10 +79,11 @@ const findNutrients = async (req, res, next) => {
};
try {
const response = await fetch(url, options);
const result = await response.json();

if(result.status == 'error') throw new Error(result.message);
if (!response.ok) {
throw new Error(response.statusText);
}

const result = await response.json();
res.status(200).json(result);
} catch (err) {
console.error(err);
Expand Down

0 comments on commit 96d6fe8

Please sign in to comment.