Implementing post_poll function in Zapier CLI development

I’m encountering an issue while using the Zapier CLI. The error states, “Results must be an array, got: object” and indicates that my result is lacking the “id” property. I realize I need to create a post_poll function to resolve this, but I’m unsure how to implement it correctly.

Below is my current function that retrieves the data:

const fetchWebinars = (z, bundle) => {
    console.log('fetching webinars...');
    let company_id = bundle.inputData.company_id;
    const options = {
        url: `https://api.example.com/v2/company/${company_id}/webinar`
    };
    
    return z.request(options)
            .then((response) => {
                return z.JSON.parse(response.content);
            });
};

The API responds with data structured like this:

{
    "webinars": [
        {
            "webinarid": 1234567,
            "starttime": "2023-10-15T14:30:00-05:00",
            "endtime": "2023-10-15T15:30:00-05:00",
            "title": "Sample Webinar",
            "status": "active",
            "companyid": 98765,
            "registrationurl": "https://example.com/register/1234567"
        }
    ],
    "totalwebinars": 1
}

I’m considering something along these lines, but I need guidance on how to register it in my application:

const afterPoll = (item, z, bundle) => {
    if(item.key === 'webinars'){
        var data = z.JSON.parse(bundle.request.data).results;
        var webinars = data.webinars.map(function(webinar){
            webinar.id = webinar.webinarid;
            return webinar;
        });
        bundle.request.data = webinars;
    }
};

module.exports = afterPoll;

Could someone assist me in understanding how to set this up correctly?

You’re misunderstanding when to use post_poll vs fixing the data structure directly. Post_poll is only for polling triggers that need to process results after fetching them. Based on your error, this looks like a regular trigger or search.

The real problem: Zapier wants your function to return an array of objects where each object has an ‘id’ field. Your fetchWebinars function returns the entire API response object, not just the webinars array. SurfingWave’s solution works for most cases, but if you really need post_poll, register it in your trigger definition:

module.exports = {
    key: 'webinar',
    noun: 'Webinar',
    display: {
        label: 'New Webinar',
        description: 'Triggers when a new webinar is created.'
    },
    operation: {
        perform: fetchWebinars,
        outputFields: [...],
        sample: {...}
    },
    afterPoll: afterPoll
};

But honestly? Just fix the data structure in your perform function. It’s simpler and easier to maintain.

your afterPoll function is almost there, but ur making it harder than it needs to be. just change ur fetchWebinars to return the array directly like everyone else mentioned. zapier wants arrays with id fields, not objects with nested arrays - that’s what’s causing the error.

You’re overcomplicating this with post_poll. The real problem is Zapier wants an array, but your API sends back an object with webinars nested inside. Just tweak your fetchWebinars function to pull out the array directly:

const fetchWebinars = (z, bundle) => {
    console.log('fetching webinars...');
    let company_id = bundle.inputData.company_id;
    const options = {
        url: `https://api.example.com/v2/company/${company_id}/webinar`
    };
    
    return z.request(options)
            .then((response) => {
                const data = z.JSON.parse(response.content);
                return data.webinars.map(webinar => ({
                    ...webinar,
                    id: webinar.webinarid
                }));
            });
};

This returns the webinars array straight up, with the id field mapped from webinarid. I’ve done this exact thing in several Zapier integrations - way cleaner than building a whole polling system.

The Problem:

You’re struggling to integrate your application with Zapier, encountering the error “Results must be an array, got: object” because your Zapier function isn’t returning data in the expected format. Your API returns an object containing a nested webinars array, while Zapier requires a flat array of webinar objects, each with an id property. You’re considering using post_poll, but a simpler solution exists.

:thinking: Understanding the “Why” (The Root Cause):

Zapier’s integrations expect a consistent data structure. Your fetchWebinars function returns a JSON object with a webinars array inside. Zapier needs a top-level array of webinar objects, each having an id property. The error arises because your function’s output doesn’t match this expectation. While post_poll could work, directly restructuring the response within fetchWebinars provides a more efficient and maintainable solution. Using post_poll adds complexity without addressing the core data structure issue.

:gear: Step-by-Step Guide:

  1. Modify fetchWebinars to Return a Flat Array: The most straightforward solution is to modify your fetchWebinars function to directly return the desired array. This eliminates the need for post_poll and simplifies your code. Replace your existing fetchWebinars function with the following:
const fetchWebinars = (z, bundle) => {
    console.log('fetching webinars...');
    let company_id = bundle.inputData.company_id;
    const options = {
        url: `https://api.example.com/v2/company/${company_id}/webinar`
    };

    return z.request(options)
            .then((response) => {
                const data = z.JSON.parse(response.content);
                return data.webinars.map(webinar => ({
                    ...webinar,
                    id: webinar.webinarid
                }));
            });
};

This revised function parses the API response, extracts the webinars array, and then uses the map function to create a new array where each webinar object includes an id property mapped from the existing webinarid property. The spread syntax (...webinar) ensures that all other properties of each webinar object are preserved.

  1. Remove afterPoll: Since we’ve directly modified fetchWebinars to provide the correct data structure, the afterPoll function is no longer necessary. Remove it entirely from your code.

  2. Test Your Zap: After making these changes, thoroughly test your Zapier integration to ensure it’s functioning correctly. Verify that the data is now being passed to subsequent steps as expected.

:mag: Common Pitfalls & What to Check Next:

  • API Changes: If your API structure changes in the future, you’ll need to update fetchWebinars accordingly. Consider creating a more robust error-handling mechanism to manage unexpected API responses.

  • Data Validation: Adding input validation to your fetchWebinars function would enhance robustness. This would check if data.webinars exists before mapping, preventing errors if the API response is malformed.

  • Alternative Approaches: While this direct approach is recommended, consider using a more general purpose transformation function for greater flexibility if you anticipate more complex data transformations in the future.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.