在 GET 请求中遍历数组

Looping over array in GET request

我正在尝试在 Zapier 中构建一个 Zap

  1. 在将 return 一个 JSON 对象的单个 GET 请求中执行。
  2. 循环遍历该对象中包含数组的键。
  3. POST 从每个数组元素到另一个数组元素的值 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);

此代码 return 是一个错误:

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 元素,但如果它能满足我的需要,我会非常高兴。

您应该返回正确的回调参数 - 第一个参数是 Errornull

差:

callback(json.arrayElem);

好:

callback(null, json.arrayElem);

callback([])callback(null, []) 的情况相同。