How do i pass API data to my content manager field on with wix fetch

Viewed 19

i am getting some data from https://www.griffati.com/restful/export/api/products.json. It's a dropping website, i want to fetch products from the API to my website.

I have made the API call, and i was successful in fetching it, i created a Database on the backend, the name of the database is Products with fields (title, name, price, image).

The issue i'm having is how to pass the data from the API to the database. I tried somethings but clearly it's not working

import { fetch } from 'wix-fetch';
import wixData from 'wix-data';


export async function getProduct() {
    const url = "https://www.griffati.com/restful/export/api/products.json";

    return fetch(url, {
        "method": "get",
        headers: {
            "Content-Type": "application/json",
   
            "Authorization": "Basic ********************************"
        }
    }).then((data) => {
        if (data.ok) {
            return data.json();
        } else {
            // return Promise.reject("Fetch failed")
            return data
        }
    }).then(data => {
        data.pageItems.forEach(item => {
            const info = {
                name: item.name,
                price: item.price,
                title: item.name
            }

            wixData.insert('Products', info)
            // console.log(item.name)
        })
    })
}

Thanks

0 Answers
Related