Fetch request doesn't work the first time. Works the second time onwards

I have a fetch request on my backend which doesn’t work the first time after staying dormant for sometime. It works second time onwards, even if I close and reopen the site. The fetch request returns a task id from a python API for async requests. Any ideas on how to solve this?

The first time I click the button for the fetch request, it basically does nothing. I don’t know what’s happening. Could it be the CORS error or something? Please let me know.

Thanks in advance

Hard to tell. If, on the first try, you keep waiting, what do you eventually get? Nothing, or an ECONNRESET? Do you use backend logging to check?

How are where are you invoking the fetch? Perhaps the first time only succeeds in “setting it up” and then subsequent calls are actually able to perform the fetch.

Nothing happens even if I keep waiting. It stays as it is. I haven’t set up the backend logging yet

I’m invoking the fetch in a backend function which I call in the frontend. Here’s my code:

export async function func ( arg1 , arg2 ) {
return fetch (URL, {

        // Adding method type 
        method :  "POST" , 
        
        // Adding body or contents to send 
        body :  JSON . stringify ({ 
            argument1 :  String ( arg1 ), 
            argument2 :  parseInt ( arg2 ) 
        }), 
        
        // Adding headers to the request 
        headers : { 
            "Content-type" :  "application/json; charset=UTF-8" 
        } 
    }) 
    . then ( response  =>  response . json ()); 

}

@bhatvishvesh8 Are you getting an error the message the first time it’s called? Is it even really being called the first time?

In order for anyone here on the forum to figure out what’s wrong, you will need to supply all of the relevant information - how are calling the backend function from the frontend?

This is how I am calling it from the frontend:

func ( String ( $w ( ‘#textBox’ ). value ), 2 ). then ( response => $w ( ‘#textBox’ ). value = response );

I tried to wrap the fetch request around a try catch block to print the error but nothing happens

Again, still need more information. Please understand that without providing sufficient information, we won’t be able to provide assistance.

How is the front end code called? When is it called? From where? Try to provide all information regarding the logic flow.

@yisrael-wix The frontend code is called with the line of code I posted above. It is called when a button is clicked. From the frontend.

@bhatvishvesh8 Without providing all of the necessary information regarding your issue, it will be impossible to provide assistance.

If you feel that you coding assistance, it is possible to find expert developers on the Wix Marketplace an online marketplace with top Velo web developers from around the world.