2016-01-22 133 views
2

我已经使用Zapier几个星期了,按照正常情况,当我们构建每个功能时,我们的需求变得更加复杂。因此,我目前在“Zapier代码”中使用了一些JavaScript。在函数中使用fetch()会导致“输出未定义”

请注意任何答案:我自己和我提供的程序员团队不是JavaScript专家,但我们确实理解大多数概念。

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

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

ReferenceError: output is not defined theFunction

ie:总的代码结构是好的,我使用fetch()和返回和处理的方式不是。

我已经尝试了一个gazillion变化,并在这里使用了一些很棒的帖子,但没有解决问题。

简化代码如下所示:

//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为我定义了'input'和'output'变量。 '输出'最初是空值,必须由代码设置一个值。

+0

这里:'回调(NULL,输出);',你会引用未定义'output'变量,从而导致错误。你的意思是它是'回调(空,响应)'? – jfriend00

+0

@ jfriend00对不起,我的错误简化了帖子的代码。固定。是的,我正在使用'callback(null,response)' –

+0

哪行代码导致错误?仅供参考,当您执行'output = {id:2};','output'时仍未定义。这将创建一个隐式的全局,除了'strict'模式,它会导致一个错误。 – jfriend00

回答

2

该功能不正确:

//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()可解决其具有用于读取主体文本或JSON分别.text().json()方法的响应对象。 .text()的解析值是string,它没有方法.json()。如果你想简单解析身体作为一个JSON使用:

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

其他任何人遇到这样的问题:从

我发现,如果我用fetch调用通过Zapier的异步返回“回调”方法在if语句中,我不得不使用“回调”从脚本中的所有其他控制路径返回,否则我会收到“ReferenceError:output is not the the Function”。我不知道为什么这个工作,但它确实。

例如,像这样的工作对我来说:

//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}); 
} 
相关问题