【问题标题】:Results must be an array, got: object in Zapier?结果必须是一个数组,得到:Zapier中的对象?
【发布时间】:2019-12-16 23:47:22
【问题描述】:

我在 Zapier 中使用搜索。我有自己的 API,当我按项目 ID 搜索项目时,它会发送一个对象。

以下是来自 API 的响应

{
    "exists": true,
    "data": {
        "creationDate": "2019-05-23T10:11:18.514Z",
        "Type": "Test",
        "status": 1,
        "Id": "456gf934a8aefdcab2eadfd22861",
        "value": "Test"
    }
}

当我通过 zap 搜索时

结果必须是一个数组,得到:对象, ({"exists":true,"data":{"creationDate":"2019-05-23T10:11:18.514Z)

下面是代码

module.exports = {
    key: 'item',
    noun: 'itemexists',
    display: {
        label: 'Find an item',
        description: 'check if item exist'
    },

    operation: {.
        inputFields: [
            {
                key: 'itemid',
                type: 'string',
                label: 'itemid',
                helpText: 'Eg. e3f1a92f72c901ffc942'
            }
        ],

        perform: (z, bundle) => {
            const url = 'http://IP:8081/v1/itemexists/';
            const options = {
                params: {
                    itemId: bundle.inputData.itemid
                }
            };


            return z.request(url, options)
                .then(response => JSON.parse(response.content));
        },
        sample: {
            "exists": true,
            "data": {
                "creationDate": "2019-05-23T10:11:18.514Z",
                "Type": "Test",
                "status": 1,
                "Id": "456gf934a8aefdcab2eadfd22861",
                "value": "Test"
    }
},
    }
};

【问题讨论】:

    标签: json rest zapier zapier-cli


    【解决方案1】:

    您从执行返回的数据必须是“数组”类型(以[ 开头。您返回了一个对象(以{ 开头的结构)。

    修复很简单 - 将返回的数据用方括号括起来。

    .then(response => [JSON.parse(response.content)]); // note the added `[]`
    
    // or, if you don't care about the `exisits` key
    .then(response => {
      const data = JSON.parse(response.content)
      return [data.data]
    });
    

    【讨论】:

    • 花了我一秒钟才明白你的意思是在返回时添加 []。谢谢。
    猜你喜欢
    • 2019-10-27
    • 2019-12-23
    • 2022-11-19
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2019-09-30
    • 2019-05-19
    相关资源
    最近更新 更多