How to use the post_poll method in Zapier CLI

老子叫甜甜 提交于 2020-01-30 08:27:24

问题


According to the docs, I should use a post_poll function to add the missing id field in the response.

How do I add the post_poll function ?

Here's my error:

Results must be an array, got: object, ({"totalevents":83,"events":[{"eventid":10266033,"c) - Got a result missing the "id" property (83)

Tried following this but it is not clear to me, I'm very new to Zapier-CLI

Update - adding code

This is the function that returns the data:

const listEvents = (z, bundle) => {

    console.log('listing events.. ');
    let client_id = bundle.inputData.client_id;
    const requestOpts = {
        url: `https://wccqa.on24.com/wcc/api/v2/client/${client_id}/event`  
    };

    return z.request(requestOpts)
            .then((response) => {
                return z.JSON.parse(response.content);
            });
};

The sample response is the following, with the distiction that I added the id param manually to avoid errors when zapier test|push:

{
    "id": 9964513,
    "eventid": 9964513,
    "archivestart": "2017-09-21T10:30:00-07:00",
    "archiveend": "2018-09-21T10:30:00-07:00",
    "description": "Zapier Event Test",
    "iseliteexpired": "N",
    "displaytimezonecd": "America/Bogota",
    "eventtype": "Live Webcam ",
    "regrequired": true,
    "clientid": 22921,
    "liveend": "2017-09-21T10:00:00-07:00",
    "createtimestamp": "2017-09-21T09:47:44-07:00",
    "audienceurl": "https://localhost.on24.com/wcc/r/9964513/C49755A02229BD48E6010848D7C81EF8",
    "lastmodified": "2017-09-21T09:47:44-07:00",
    "livestart": "2017-09-21T08:45:00-07:00",
    "goodafter": "2017-09-21T09:00:00-07:00",
    "regnotificationrequired": true,
    "isactive": true,
    "localelanguagecd": "en"
}

The ACTUAL response from the endpoint the following which is used in the app created in the Web Builder App instead of CLI and works fine:

{
    "events": [
        {
            "eventid": 9964513,
            "archivestart": "2017-09-21T10:30:00-07:00",
            "archiveend": "2018-09-21T10:30:00-07:00",
            "description": "Zapier Event Test",
            "iseliteexpired": "N",
            "displaytimezonecd": "America/Bogota",
            "eventtype": "Live Webcam ",
            "regrequired": true,
            "clientid": 22921,
            "liveend": "2017-09-21T10:00:00-07:00",
            "createtimestamp": "2017-09-21T09:47:44-07:00",
            "audienceurl": "https://localhost.on24.com/wcc/r/9964513/C49755A02229BD48E6010848D7C81EF8",
            "lastmodified": "2017-09-21T09:47:44-07:00",
            "livestart": "2017-09-21T08:45:00-07:00",
            "goodafter": "2017-09-21T09:00:00-07:00",
            "regnotificationrequired": true,
            "isactive": true,
            "localelanguagecd": "en"
        }
    ],
    "totalevents": 1
}

I was thinking something along the line of the following, but how do I register this ?

const postPoll = (event,z,bundle) => {

    if(event.key === 'events'){

        var results = z.JSON.parse(bundle.request.data).results;

        var events = results.events.map(function(event){
                              event.id = event.eventid;
                              return event;
                            });     

        bundle.request.data = events;
    }
};

module.exports = postPoll;

回答1:


Nice, so you're almost there! CLI apps don't have pre_ and post_ poll methods. Instead, you put any manipulation after the response comes in.

const listEvents = (z, bundle) => {
console.log('listing events.. ');
let client_id = bundle.inputData.client_id;
const requestOpts = {
    url: `https://wccqa.on24.com/wcc/api/v2/client/${client_id}/event`  
};

return z.request(requestOpts)
        .then((response) => {
            return z.JSON.parse(response.content);
        })
        .then(data => {
            const events = data.events; // array of events
            return events.map(function(e){ // returns array of objects with `id` defined
                e.id = e.event_id
                return e
            }) 
        })
};


来源:https://stackoverflow.com/questions/47837434/how-to-use-the-post-poll-method-in-zapier-cli

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!