Unable to get the fetched result in database

Hi, I’m trying to insert the response after fetch in the database but it inserts an empty object.
I am able to see the required result in my site logs but when inserted to the database it’s empty.


            fetch(body['endpoint'], {
                    "method": "get",
                    "headers": {
                        "Content-Type": "application/json",
                        "Authorization": 'Bearer' + body['accessToken'],
                        "Cache-Control": "no-cache"
                    }
                })
                .then((httpResponse) => {
                    if (httpResponse.ok) {
                        r = httpResponse.json()
                        let options = {
                            "suppressAuth": true,
                            "suppressHooks": true
                        };
                        let tI = {
                            "data": r
                        }
                        wixData.insert("truecaller", tI, options);
                        return r
                    } else {
                        return Promise.reject("Fetch did not succeed");
                    }
                })
                    

Here’s the screenshot of my database:

Screenshot of my site logs:

Thanks.

Hi! Make sure that the fetch request is actually returning data and that the r variable is not empty. You can add a console.log(r) statement after the r = httpResponse.json() line to check the value of r. Show that the tI object you are passing to the wixData.insert function is correct. You could add a console.log(tI) statement before the wixData.insert line to check the value of tI. Is the wixData.insert function is being called correctly and that there are no errors being thrown? You could add a .catch((error) => console.log(error)) block after the wixData.insert line to catch any errors that may be occurring. Check out: https://www.wix.com/corvid/reference/wix-data.html#insert

Hi! I’ve already tried putting console.log() in multiple places. What I have observed is I can see the value of r only once no matter how many times I print it. I don’t understand why r can only be used once!!
The wixData.insert is being called correctly as I can see {} in the database each time after fetch.

Here, I have console.log(r), console.log(tI) and return r

The logs (end to start)

I think the issue you are experiencing, where the value of r is only printed once, is likely due to the fact that the fetch function is asynchronous, meaning that the code after it will be executed before the HTTP request has completed. This means that the value of r may not be set when you try to print it multiple times. To fix this, you can move the code that relies on the value of r inside the then block, so that it is only executed after the HTTP request has completed and the value of r has been set.

Hi! Thanks for the reply.
But the issue was that I was trying to insert the wrong response.
I was using httpResponse.json() instead of json (in the second .then())


            fetch(body['endpoint'], {
                    "method": "get",
                    "headers": {
                        "Content-Type": "application/json",
                        "Authorization": 'Bearer' + body['accessToken'],
                        "Cache-Control": "no-cache"
                    }
                })
                .then((httpResponse) => {
                    if (httpResponse.ok) {
                      //I was trying to insert httpResponse.json()
                        return httpResponse.json()
                    } else {
                        return Promise.reject("Fetch did not succeed");
                    }
                })
                .then((json) => {
                  //What I should insert was json
                    let options = {
                        "suppressAuth": true,
                        "suppressHooks": true
                    };
                    let tI = {
                        "data": json
                    }
                    wixData.insert("truecaller", tI, options);
                })
                .catch(err => console.log(err));

            return ok(options)
        })