Plugin not returning reponse Need Help

I am creating a plugin that will have different actions to return different sets of data objects from a external API. I am not using the API connector because the authentication requires a HmacSHA256 signature in the API url so this is why I have went with creating a plugin to make the API request and then return the data. For the first API call I am testing how to return the country list from this API. I am able to see that the plugin is successfully requesting and receiving data from the API and logging it to the server logs. Please see my code below. Any help would be greatly appreciated.

Followed example for recreating a Dummy API call and manually created the object for the response.

Setup my parameters and response based on the article.

Here is my code:
Like I said I have validated that the code is working and returning results as you can see below in my response. I can’t figure out how to return it from the workflow to the UI

async function(properties, context) {

try 
{
    const CryptoJS = require('crypto-js');
    const base64url = require('base64-url'); // Import the base64-url library
    const axios = require('axios');

    // set vars
    const apiUrl = context.keys['ApiUrl'];
    const apiKey = context.keys['ApiKey'];
    const apiVersion = context.keys['ApiVersion'];
    const responseFormat = context.keys['ResponseFormat'];
    const sigVersion = context.keys['SigVersion'];
    const sigMethod = context.keys['SigMethod'];
    const secretKey = context.keys['SecretKey'];

    // creates the param string that will be used to calculate signature
    function getUrlString(actionCode, timestampStr) {
        return `${actionCode}/${apiKey}/${apiVersion}/${sigVersion}/${sigMethod}/${timestampStr}/${responseFormat}`;
    }

    // calculates and returns the signature
    function getSignature(toSignStr) {
        const utf8EncodedToSignStr = utf8Encode(toSignStr);
        const utf8EncodedSecretKey = utf8Encode(secretKey);
        return calculateHMACSHA256(utf8EncodedToSignStr, utf8EncodedSecretKey);
    }

    // generate the timestamp in "yyMMddHHmmssZZZZZ" format
    function generateTimestamp() {
        const now = new Date();
        const year = now.getFullYear().toString().slice(-2); // Get the last 2 digits of the year
        const month = (now.getMonth() + 1).toString().padStart(2, '0'); // Month is zero-based, so add 1 and pad with 0 if needed
        const day = now.getDate().toString().padStart(2, '0');
        const hours = now.getHours().toString().padStart(2, '0');
        const minutes = now.getMinutes().toString().padStart(2, '0');
        const seconds = now.getSeconds().toString().padStart(2, '0');
        const timezoneOffsetMinutes = now.getTimezoneOffset(); // Get the timezone offset in minutes
        const timezoneOffsetHours = Math.abs(Math.floor(timezoneOffsetMinutes / 60)); // Convert minutes to hours

        // Calculate the timezone offset in the format ±HH:MM
        const timezoneOffsetSign = timezoneOffsetMinutes < 0 ? '+' : '-';
        const timezoneOffset = `${timezoneOffsetSign}${timezoneOffsetHours.toString().padStart(2, '0')}:${(Math.abs(timezoneOffsetMinutes) % 60).toString().padStart(2, '0')}`;

        // Combine all components to form the timestamp
        const timestamp = `${year}${month}${day}${hours}${minutes}${seconds}${timezoneOffset}`;

        return timestamp;
    }

    // convert a string to UTF-8 encoded bytes
    function utf8Encode(input) {
        return decodeURIComponent(encodeURIComponent(input));
    }

    // calculate HMAC-SHA256 signature
    function calculateHMACSHA256(data, key) {
        const hash = CryptoJS.HmacSHA256(data, key);
        const signature = base64url.escape(hash.toString(CryptoJS.enc.Base64));
        return signature;
    }
    
    const actionCode = "CountryList";
    const timestampStr = generateTimestamp();
    const toSignStr = getUrlString(actionCode, timestampStr);
    const signature = getSignature(toSignStr);

    // generate parameters
    var params = `${apiKey}/${apiVersion}/${sigVersion}/${sigMethod}/${signature}/${timestampStr}/${responseFormat}`;
    
    // request countryList
    const response = await axios.post(`${apiUrl}/${actionCode}/${params}`, {
        id_continent: properties.continent
    });
    
    if (response.data.Status === 1) {
      console.log("Response was successful");
      
      let resultData = [];
      response.data.countryList.forEach((country) => {
          resultData.push({
              "id_continent": country.id_continent,
              "id_country": country.id_country,
              "countryFull": country.countryFull,
              "countryShort": country.countryShort
            });
      });
      console.log(resultData);
      return resultData;
        
    } else {
      console.log("Error was logged");
      throw new Error(response.data.message);
    }
    
} catch (error) {
    console.log("Error: " + error);
    return error;
}}

Server logs:

console.log(response);
Plugin server side action console output

535, countryFull: ‘Bonaire, Sint Eustatius and Saba’, countryShort: ‘BES’ }, { id_continent: ‘NA’, id_country: 92, countryFull: ‘British Virgin Islands’, countryShort: ‘VGB’ }, { id_continent: ‘NA’, id_country: 124, countryFull: ‘Canada’, countryShort: ‘CAN’ }, { id_continent: ‘NA’, id_country: 136, countryFull: ‘Cayman Islands’, countryShort: ‘CYM’ }, { id_continent: ‘NA’, id_country: 188, countryFull: ‘Costa Rica’, countryShort: ‘CRI’ }, { id_continent: ‘NA’, id_country: 895, countryFull: ‘Cuba’, countryShort: ‘CUB’ }, { id_continent: ‘NA’, id_country: 531, countryFull: ‘Curaçao’, countryShort: ‘CUW’ }, { id_continent: ‘NA’, id_country: 212, countryFull: ‘Dominica’, countryShort: ‘DMA’ }, { id_continent: ‘NA’, id_country: 214, countryFull: ‘Dominican Republic’, countryShort: ‘DOM’ }, { id_continent: ‘NA’, id_country: 222, countryFull: ‘El Salvador’, countryShort: ‘SLV’ }, { id_continent: ‘NA’, id_country: 304, countryFull: ‘Greenland’, countryShort: ‘GRL’ }, { id_continent: ‘NA’, id_country: 308, countryFull: ‘Grenada’, countryShort: ‘GRD’ }, { id_continent: ‘NA’, id_country: 312, countryFull: ‘Guadeloupe’, countryShort: ‘GLP’ }, { id_continent: ‘NA’, id_country: 320, countryFull: ‘Guatemala’, countryShort: ‘GTM’ }, { id_continent: ‘NA’, id_country: 332, countryFull: ‘Haiti’, countryShort: ‘HTI’ }, { id_continent: ‘NA’, id_country: 340, countryFull: ‘Honduras’, countryShort: ‘HND’ }, { id_continent: ‘NA’, id_country: 388, countryFull: ‘Jamaica’, countryShort: ‘JAM’ }, { id_continent: ‘NA’, id_country: 474, countryFull: ‘Martinique’, countryShort: ‘MTQ’ }, { id_continent: ‘NA’, id_country: 484, countryFull: ‘Mexico’, countryShort: ‘MEX’ }, { id_continent: ‘NA’, id_country: 500, countryFull: ‘Montserrat’, countryShort: ‘MSR’ }, { id_continent: ‘NA’, id_country: 558, countryFull: ‘Nicaragua’, countryShort: ‘NIC’ }, { id_continent: ‘NA’, id_country: 0, countryFull: ‘Other’, countryShort: ‘OTH’ }, { id_continent: ‘NA’, id_country: 591, countryFull: ‘Panama’, countryShort: ‘PAN’ }, { id_continent: ‘NA’, id_country: 630, countryFull: ‘Puerto Rico’, countryShort: ‘PRI’ }, { id_continent: ‘NA’, id_country: 652, countryFull: ‘Saint Barthélemy’, countryShort: ‘BLM’ }, { id_continent: ‘NA’, id_country: 659, countryFull: ‘Saint Kitts and Nevis’, countryShort: ‘KNA’ }, { id_continent: ‘NA’, id_country: 662, countryFull: ‘Saint Lucia’, countryShort: ‘LCA’ }, { id_continent: ‘NA’, id_country: 663, countryFull: ‘Saint Martin’, countryShort: ‘MAF’ }, { id_continent: ‘NA’, id_country: 666, countryFull: ‘Saint Pierre and Miquelon’, countryShort: ‘SPM’ }, { id_continent: ‘NA’, id_country: 670, countryFull: ‘Saint Vincent and the Grenadines’, countryShort: ‘VCT’ }, { id_continent: ‘NA’, id_country: 534, countryFull: ‘Sint Maarten (Dutch part)’, countryShort: ‘SXM’ }, { id_continent: ‘NA’, id_country: 780, countryFull: ‘Trinidad and Tobago’, countryShort: ‘TTO’ }, { id_continent: ‘NA’, id_country: 796, countryFull: ‘Turks and Caicos Islands’, countryShort: ‘TCA’ }, { id_continent: ‘NA’, id_country: 840, countryFull: ‘United States of America’, countryShort: ‘USA’ }, { id_continent: ‘NA’, id_country: 850, countryFull: ‘United States Virgin Islands’, countryShort: ‘VIR’ } ] END RequestId: 4bf8016f-7fc8-4a4d-985a-2ecc97865d01 REPORT RequestId: 4bf8016f-7fc8-4a4d-985a-2ecc97865d01 Duration: 187.66 ms Billed Duration: 188 ms Memory Size: 128 MB Max Memory Used: 97 MB

Error:

Workflow error - Plugin action iGolf GetCountryList error when parsing the response: the key 0 is not part of the btype definition

Workflow

Design

Thank you for your time and I hope I have provided all information necessary. I’ve been beating my head against the wall on this and would like to figure this out. Otherwise I will create a separate API to do this for me and deploy to Google Cloud.

1: if you are comfortable in gCloud, do this there. it’s faster and cheaper
2: in your return object, you need to prefix in your code with p

start with these 2 ideas

  1. I think you meant _p_ but markdown got into your reply
1 Like

Yes! Thank you

1 Like

hey thanks for the response but when I add “p” to the beginning of the object properties I get the same error returned from the workflow.

Plugin action GetCountryList error when parsing the response:
the key 0 is not part of the btype definition

Any other ideas?