2016-01-22 137 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}); 
} 
相關問題