Lấy api bằng fetch đơn giản (ok)

https://www.youtube.com/watch?v=9EkzraGTQ_M

const apiGetAllFoods = 'http://localhost:3001/list_all_foods';
async function getFoodsFromServer() {
    try {
        let response = await fetch(apiGetAllFoods);
        let responseJson = await response.json();
        return responseJson.data; //list of foods
    } catch (error) {
        console.error(`Error is : ${error}`);
    }
}
export {getFoodsFromServer};

Last updated

Was this helpful?