Hi, i’m not a developer and have been trying to get this to work, but am having issues. I believe it is because i am not defining which item in the array to use, but not sure.
I’m using wix fetch on the backend and receiving a httpResponse but can’t figure out how to replace a text element with the first JSON array object.
backend:
import wixSecretsBackend from ‘wix-secrets-backend’;
import {fetch} from ‘wix-fetch’;
export async function getFlowersJson() {
const secret = await
let headers = {
‘Authorization’: 'Basic ’ + Buffer.from(secret + “:” + pw).toString(‘base64’),
‘Content-Type’: “application/json”,
‘Access-Control-Allow-Origin’: ""*
};
return [fetch("https://www.floristone](fetch("https://www.floristone) . com/api/rest/flowershop/getproducts?category=md", {
“method”: “GET”,
“headers”: headers})
.then((httpResponse) => {
if(httpResponse.ok){
return httpResponse.json();
} else {
return Promise.reject(“Fetch did no succeed”)
}
})
}
Response data:
Front end:
trying to replace the “#textMain” element with the first array object description:
import {getFlowersJson} from ‘backend/florist-one-api’;
export function button1_click_1(event) {
getFlowersJson()
.then((json) => {
const products = Object.values(json)[0];
console.log(json);
$w(“#textMain”).text = String(products.description);
} )
.catch(err => {
console.log(err);
} )
}
Currently when i click the button, the text gets replaced with “Undefined”
Any help would be appreciated.
Thanks,
Will