Integration issue

Hi Corvid Forum,

We are doing an API integration and are running into an issue. We are getting back two responses from the WIX code and we just need one response for the integration to work. The second response (automatic refresh) makes it so we are losing the integration token. Do you have a solution?

The issue further explained:
The first response is received right away
The second response is being delivered after the entire content of the page has been loaded (2-5 seconds later)

Reviewing the code, we see the following:

// For full API documentation, including code examples, visit http://wix.to/94BuAAs

$w.onReady(function () {
//TODO: write your page related code here…

});

Which means the correct way would be to receive only one response → when entire content is loaded
No response should be provided until the page is loaded.

Please review the WIX code for the homepage:

import wixLocation from ‘wix-location’;
import { getCleverToks } from ‘backend/app.jsw’;
// …
$w.onReady( function () {
let query = wixLocation.query; //

if (query[‘code’]) {
console.log(query[‘code’]);
getCleverToks(query[‘code’])
.then((json) => {
console.log(json);

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

});
**app.jsw - **

import { fetch } from ‘wix-fetch’;
export function getCleverToks(code) {
return fetch(’ https://bookjobsnow.com/app_clever.php?code= ’ + code, {
“method”: “get”,
“headers”: {
“Authorization”: “Bearer 123”
}
})
.then((httpResponse) => {
return “Bearer 123”;
});
}

thank you for your help in fixing this issue,
Dan

Hi Dan,
I build a simple site with the code above code and I got only one response, “Bearer 123”, after $w.onReady was called and the site was rendered, as expected. can you supply more info on how to replicate the problem?

“We save all test results in our database.
Looks like Andres ran a few tests around 2pm yesterday
As you can see on the image below, test results are not in our favor, there are still two responses for all 3 tests
note: you need to run the test from the browser using the link https://bookjobsnow.com/test.php ,
not from the console”

Hi Dan,

This happens because, the site is rendered twice first on the server side (for faster load time), and then the on the client side so the $w.onReady is called twice. you can check where the code is running (server or client) by simple adding the following condition to the $w.onReady method:

import wixLocation from ‘wix-location’;
import { getCleverToks } from ‘backend/app.jsw’;
===> import wixWindow from ‘wix-window’;

// …
$w.onReady( function () {

==> if (wixWindow.rendering.env === “browser”) {
let query = wixLocation.query; //
if (query[‘code’]) {

    console.log(query['code']); 
    getCleverToks(query['code']) 
        .then((json) => { 
            console.log(json); 

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

}});

I think this may work. Thank you, Andres. We still have to do some tests, but I really appreciate your reply!

Now, I have to attend to the custom login light landing on the incorrect page once a user logs in…