2016-12-14 102 views
0

我想在Zapier將循環在數組中的GET請求

  1. 執行在單GET請求,將返回一個JSON對象建立一個扎普。
  2. 循環包含數組的對象中的鍵。
  3. 將每個數組元素的值發佈到另一個URL。

我最初嘗試使用原生Zapier元素來做這件事,但我不確定如何對每個數組項目採取行動。我的Zapier代碼文檔中寫着:

Setting the output to an array of objects will run the subsequent steps multiple times — once for each object in the array.

所以我現在在做一個碼元的GET請求:

fetch('https://domain.com/path', { headers: { 'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxx'} }) 
    .then(function(res) { 
     return res.json(); 
    }).then(function(json) { 
     if (typeof json.arrayElem == 'object') { 
      callback(json.arrayElem); 
     } else { 
      callback([]); 
     } 
    }).catch(callback); 

此代碼返回一個錯誤:

Bargle. We hit an error creating a run javascript. :-(Error: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

我但我認爲我很接近,因爲如果我將callback(json.arrayElem);更改爲callback(json);,我會得到相同的錯誤,但只有一個[object Object]而不是多個。

任何人有任何指針?我沒有結婚使用Code元素,但非常高興,如果它會做我需要的。

回答

1

您應該返回適​​當的回調參數 - 第一個參數是Errornull

壞:

callback(json.arrayElem); 

好:

callback(null, json.arrayElem); 

callback([])callback(null, [])同一案件。