Javascript 在函数中使用fetch()会导致;“输出未定义”;

Javascript 在函数中使用fetch()会导致;“输出未定义”;,javascript,asynchronous,zapier,fetch-api,Javascript,Asynchronous,Zapier,Fetch Api,我已经使用Zapier几个星期了,按照正常情况,随着我们构建每个功能,我们的需求和需求变得更加复杂。因此,我当前的问题是在“Zapier编写的代码”中使用一些JavaScript 请注意:我和我的程序员团队不是JavaScript专家,但我们确实理解大多数概念 目标:仅当javascript的输入为true时才进行fetch()调用 问题:当代码中包含fetch()时,Zapier总是返回“output not defined” ReferenceError:函数未定义输出 ie:总体代码结构还

我已经使用Zapier几个星期了,按照正常情况,随着我们构建每个功能,我们的需求和需求变得更加复杂。因此,我当前的问题是在“Zapier编写的代码”中使用一些JavaScript

请注意:我和我的程序员团队不是JavaScript专家,但我们确实理解大多数概念

目标:仅当javascript的输入为true时才进行fetch()调用

问题:当代码中包含fetch()时,Zapier总是返回“output not defined”

ReferenceError:函数未定义输出

ie:总体代码结构还可以,但我使用fetch()和返回处理的方式却不行

我尝试了无数的变化,并利用了一些伟大的职位在这里,但没有解决这个问题

简化的代码如下所示:

 //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};
}
我相信这是我从fetch()返回的方式,或者是JavaScript的异步特性


注意:Zapier为我预定义了“输入”和“输出”变量“输出”最初为空,必须由代码设置值。

此函数不正确:

//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.
        });
}

对于遇到此问题的任何其他人:

我发现,如果我在if语句中使用fetch调用通过Zapier的“callback”方法异步返回,那么我必须使用“callback”也从脚本中的所有其他控制路径返回,否则我将收到“ReferenceError:函数未定义输出”。我不知道这为什么有效,但它确实有效

例如,类似这样的事情对我很有用:

    //This does the Fetch:
function doFetch(urlAddress) {
    console.log("2. Doing Fetch");
    return fetch(urlAddress)
        .then(function(res) {
            return res.text();
        });
}

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};
    //Use 'callback' instead of output
    callback(null, {id: 2});
}

此处:
回调(null,输出),您正在引用未定义的
输出
变量,因此会导致错误。你的意思是
回调(null,response)
?@jfriend00对不起,我在简化帖子代码时输入了错别字。固定的。是,我正在使用
回调(null,response)
哪行代码导致了错误?仅供参考,当您执行此操作时,
output={id:2}
输出
仍未定义。这将创建一个隐式全局变量,除非在
严格模式下,否则会导致错误。具体来说,我不知道,这是我的问题。但是如果我从函数中删除fetch()并只返回一个已知的响应,例如:
return{id:1}然后它就工作了。所以它变成了编码101。因此,我认为这是我调用异步fetch()并从中返回的方式,以及它与其他语句的交互方式导致了问题。您需要添加try/catch处理程序并输出异常,以确定是哪一行代码导致了问题。您必须调试该问题才能获得更多信息。或者,您可以设置断点并单步执行所有相关代码,以查看它抛出的位置。调试通常是一个系统化的过程,使用调试工具或代码插入工具收集信息,然后缩小原因范围,而不是一个我们可以通过查看代码进行猜测的过程。下一步是找到导致错误的确切代码行。
    //This does the Fetch:
function doFetch(urlAddress) {
    console.log("2. Doing Fetch");
    return fetch(urlAddress)
        .then(function(res) {
            return res.text();
        });
}

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};
    //Use 'callback' instead of output
    callback(null, {id: 2});
}