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"
    }
},
    }
};
xavdid
  • 5,092
  • 3
  • 20
  • 32
TechChain
  • 8,404
  • 29
  • 103
  • 228

2 Answers2

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]
});
xavdid
  • 5,092
  • 3
  • 20
  • 32
  • 1
    Took me a second to see that you mean to literally add [] at return. Thank you. – Jak Apr 14 '21 at 14:41
0

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.

Michael F.
  • 922
  • 1
  • 6
  • 10