Fetch API and store it in a collection

Hi

It is pretty straight-forward if you already have successfully fetched. You just need to map the values you’re interested into an object and save it to the correct collection.

Example below.

let res = null;
	// FETCHING
  try {
		res = await fetch (wixRestaurantAPIUrl + "organizations/666666666677777/orders?viewMode=restaurant",{ "method": "GET",	"headers": { "Authorization": "Bearer " + accessToken}});
	}
	catch(error)
	{
			await log("", "", error.message, "ERROR");
			return;
	}
  
  // MAPPING VALUES TO OBJECT
  let commande = {
		"orderId":jsonWix["value"]["results"][0]["id"],
		"clientName":jsonWix["value"]["results"][0]["contact"]["firstName"] + " " + jsonWix["value"]["results"][0]["contact"]["lastName"],
		"clientPhone":jsonWix["value"]["results"][0]["contact"]["phone"]
	};
  // INSERTING
  wixData.insert("Commandes", commande)
	.then( (results) => {
		log(commande["orderId"], "", "Commande créée", "INFO");
		return;
	} );