Return value from Back end function

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 !!

Regards,
Vasil

Try placing a return before the fetch:

return fetch(fullUrl...

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

You should be able to do it in the then()s of the promise. You can append another .then() if necessary.

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

Hi vassil.dimitrov,

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):

return fetch(url, {method: ‘post’})
.then(response => response.json())

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
});

Hope this helps!

Hey Sam,

Can you help me in returning the response from Stripe when using NPM

 
export function subscribe(cus, items) {

key.subscriptions.create({
  customer: cus,
  items: items
}, function(err, subscription) {
 if(err) {
      console.log('Error');
    }
      console.log('Jackpot');
  }
);
}

Console.log is working but when I try to return err; or return subscription; then nothing happens.

Can you help please

Hey, for me it doesn´t seems to work:
BKD file:

return getJSON(url)
.then(json => {

var events = json.recomms;

// delete the old key id and add the key _id
var str = “filler”
str = JSON.stringify(events);

str = str.replace(/"id":confused: g , “"_id":” );
events = JSON.parse(str);
return events;

})
. catch (err => {}
//console.log(err)
);

and:
Frontend:

var events = await getRecommendations()

console.log(events)

The problem is that in the Frontend code the variable events is still undefined.