Hello,
I am new at Wix and sorry if this sounds stupid but I have tried to find solution and failed so far.
I am using wix-fetch function to get response for remote API and this works fine.
But I could not assign received object to variable which my function returns…
Sound really basic but doesn’t work… Here is the code:
import {fetch} from ‘wix-fetch’;
export function connectremoteAPI() {
var fullUrl = ‘https://api…json’;
var proxy;
fetch(fullUrl, {method: ‘get’})
.then(response => response.json())
.then((json) => {console.log(json.Obejctxxxxxx);});
return(proxy);
}
It neither prints anything in the console neither returns value of proxy variable.
Any help will be greatly appreciated !!
Thank you Sam,
It works this way , but the problem is that I want to do export object received from API call to a variable at backend module end make some calculations on it …
Only after those calculations I need to return data and handling to front end module…
Is this possible in any way ?
Regards,
Vasil
Hello,
here is what I tried as you advised above but it still fails - I get nothing in Console :
return fetch(fullUrl, {method: ‘get’})
//.then(response => response.json())
.then((json) => {
console.log(“here 3”);
console.log(JSON.stringify(json));
proxy = JSON.stringify(json);
console.log(proxy);
});
Any ideas what is wrong here ?
REgards,
Vasil
I got the same problem as you, took me a while, but I got it working as below, might not be the best solution, but it worked for me. Any other suggestion/improvement welcomed.
Original working code (in one of the backend jsw):
Then, if I insert any console.log in it, it failed to return to the caller. Found that if I add an extra “return json” at the end, it works.
return fetch(url, {method: ‘post’})
.then(response => response.json())
//add this extra “then” to dump received result from fetch call
.then((json) => {
for(let key2 in json)
{
console.log("result - "+key2 + “:” + json[key2]);
}
return json; //adding this extra return fixed the problem
});