.json() promise never gets resolved in events.js

Hi everyone !

In order to do some debugging, I gathered all my functions in one single piece of code.

This is the return part of my event hook callback function wixStores_onOrderPaid(event)

// 1) [ api_url & requestOptions are defined here ]
// 2) The whole api call works on the back end

 return fetch(api_url, requestOptions)
        .then((httpResponse) => {
            console.log("CONSOLE DISPLAY 1"); // <- This Displays
            console.log(httpResponse);
        if (httpResponse.ok) {
                 console.log("CONSOLE DISPLAY 2"); // <- This Displays too !
                console.log("THE RESPONSE :::",httpResponse); // <- This Displays as (a) (see right under this box)
            return httpResponse.json().then( (a) => {
                    console.log("AAAAAAAAAA ",a);  // <- This DOES NOT DISPLAY IN CONSOLE
                 return Promise.resolve(a); //<- This never resolves :( 
                });
            } else {
                 return Promise.reject("Fetch did not succeed");
            }

        })
        .catch((z) => { console.log(z); return z })

(a) : {“url”:“https://MYHIDDENAPI_URL.com”,“status”:200,“statusText”:“OK”,“headers”:{“_headers”:{“content-type”:[“application/json; charset=utf-8”],“etag”:[“W/"24043-XSZSHLYnb+P4eIZIstxQjRFuKLU"”],“function-execution-id”:[“izit46fjdeqd”],“vary”:[“Origin”],“x-powered-by”:[“Express”],“x-cloud-trace-context”:[“a7c73af9fd0fdf361;o=1”],“date”:[“Wed, 17 Feb 2021 14:10:04 GMT”],“server”:[“Google Frontend”],“content-length”:[“147523”],“connection”:[“close”]}},“ok”:true,“body”:{“_readableState”:{“objectMode”:false,“highWaterMark”:16384,“buffer”:{“head”:{“data”:{“type”:“Buffer”,“data”:[123,34,109,121,100,“next”:{“data”:{“type”:“Buffer”,“data”: [LONG SERIES OF NUMBERS…[66,103,65,65,65,65,65,65,65,6

I don’t understand why the json() part never gets resolved, even though the API server works fine, and I get a result if I use the api call on the front end…

Any help is most welcome!

Raphaël

Try:

return fetch(api_url, requestOptions)
        .then((httpResponse) => {
            console.log("CONSOLE DISPLAY 1"); 
            console.log(httpResponse);
	if(!httpResponse.ok){return Promise.reject("Fetch failed");}
         console.log("CONSOLE DISPLAY 2");
           console.log("THE RESPONSE :::",httpResponse);
           return httpResponse.json();
})
.then(a => {
        console.log("AAAAAAAAAA ",a);  
      return a;
                })
.catch(err => console.log(err));

Hi thanks J.D
Unfortunately, It does not work :frowning:

I’ve been trying to fix this problem since saturday,
Is there sth in particular to know in order to use wix-fetch in events.js??

“CONSOLE DISPLAY 2:” is the last input in the console I see.

I believe the problem has to do with the way wix-fetch deal with the response, which is a buffer and can contain between 500k and 1million chars.
I don’t know if it is too much? It is a base64 image.

I don’t know. In the past I had problems with sending http requests containing large base64 data and was told by the Wix Team that’s it’s limited to 512kb.
Maybe that’s related to your issue but it doesn’t explain why it logged the “CONSOLE DISPLAY 2” and not the next line.
Have you tried it with small images?

@jonatandor35 Yes I’ve finally managed to deal with my problem by reducing the size of what is sent by my api to half the size :slight_smile: