Skip to content

Commit

Permalink
Update src/service/apiCall.ts
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
Loule95450 and coderabbitai[bot] authored Feb 27, 2024
1 parent f1fac40 commit 4b035e4
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/service/apiCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ export async function getProduct(barcode: string) {
}
let data: ProductData | null = null;

await fetch(apiUrl + barcode)
.then(result => result.json())
.then(json => {
data.code = json.code;
data.name = json.product.generic_name_fr
? json.product.generic_name_fr
: json.product.generic_name_en
? json.product.generic_name_en
: json.product.generic_name;
data.nutriscore = json.product.nutriscore_grade;
data.categories = json.product.categories;
data.nutriments = json.product.nutriments;
})
.catch(error => {
console.error(error);
data = null;
});
try {
const response = await fetch(apiUrl + barcode);
const json = await response.json();
data.code = json.code;
data.name = json.product.generic_name_fr
? json.product.generic_name_fr
: json.product.generic_name_en
? json.product.generic_name_en
: json.product.generic_name;
data.nutriscore = json.product.nutriscore_grade;
data.categories = json.product.categories;
data.nutriments = json.product.nutriments;
} catch (error) {
console.error(error);
data = null;
}

return data;
}

0 comments on commit 4b035e4

Please sign in to comment.