Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fetch() in function results in "output not defined"

I have been using Zapier for several weeks now and as per normal, our needs and wants have become more complex as we build each feature. Hence my current issue with some JavaScript in a "Code by Zapier".

Note for any answer: Myself and the team of programmers I have available are not JavaScript experts but we do understand most concepts.

Aim: To make a fetch() call only if an input to the javascript is true.

Problem: Zapier always returns "output not defined" when the fetch() is included in the code.

ReferenceError: output is not defined theFunction

ie: The overall code structure is ok, the way I am using fetch() and returning and processing is not.

I have tried a gazillion variations and utilised some great posts here, but nothing solved the issue.

The simplified code looks like this:

 //This does the Fetch:
function doFetch(urlAddress) {
    console.log("2. Doing Fetch");
    return fetch(urlAddress)
        .then(function(res) {
            return res.text();
        })
        .then(function(response) {
            //Hardcode a return.
            console.log("3. Returning Response");
            return response.json();
        });
}

if (input.emailHasChanged == "true") 
{
    //Do Something
    console.log("1. Updating Email");
    doFetch(urlAddress)
        .then(function(response) {
            //Do something with Reponse.
            console.log("4. Doing Something: ", response);
            callback(null, response);
        });
}
else
{
    //Do Nothing
    console.log("1. Not Updating email");
    output = {id: 2};
}

I believe it is the way I am either returning from the fetch() or the asynchronous nature of JavaScript.

Note: Zapier predefines the 'input' and 'output' variables for me. 'output' is initially Null and a value must be set by the code.

like image 684
Look Left Avatar asked Feb 03 '26 21:02

Look Left


1 Answers

This function is incorrect:

//This does the Fetch:
function doFetch(urlAddress) {
    console.log("2. Doing Fetch");
    return fetch(urlAddress)
        .then(function(res) {
            return res.text();
        })
        .then(function(response) {
            //Hardcode a return.
            console.log("3. Returning Response");
            return response.json(); // <-- response is an string. This method is not defined.
        });
}

fetch() resolves in a response object which has methods for reading the body as text or JSON with .text() and .json() respectively. The resolving value for .text() is a string that does not have the method .json(). If you want to simply parse the body as a JSON use:

//This does the Fetch:
function doFetch(urlAddress) {
    console.log("2. Doing Fetch");
    return fetch(urlAddress)
        .then(function(res) {
            return res.json();
        });
}
like image 128
Salva Avatar answered Feb 06 '26 09:02

Salva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!