Results must be an array, got: object in Zapier?
Asked Answered
K

2

5

I am using search in Zapier. I have my own API which sends a single object when I search item by its item Id.

Below is response from API

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

when i search this by zap

Results must be an array, got: object, ({"exists":true,"data":{"creationDate":"2019-05-23T10:11:18.514Z)

Below is the code

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"
    }
},
    }
};
Kaunas answered 9/8, 2019 at 8:53 Comment(0)
D
8

The data you return from your perform must be of type "Array" (which starts with [. You returned an object (a structure starting with {).

The fix is simple enough - wrap your returned data in square brackets.

.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]
});
Drawback answered 9/8, 2019 at 14:28 Comment(1)
Took me a second to see that you mean to literally add [] at return. Thank you.Alveta
S
1

If you didn't want to or can't alter your existing codebase, you can also parse the data retrieved from your APIs on Zapier's end.

In the API Configuration step, if you switch to Code Mode rather than Form Mode, you can wrap the results from the API call in [].

Zapier platform showing Code Mode, with results wrapped in array brackets.

Sidhu answered 17/5, 2023 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.