Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Can't determine issue with JS storeClient function and error thrown

I have created two async functions within my JavaScript Zapier step to parse a raw webhook. The two async functions center around storeClient to "get" and "set" values on Storage by Zapier. However, I get an error thrown. I have a screen shot from Zapier test sub-step. Due to the way Zapier returns on error, I can't see any of my console.log output.

What have I done wrong with the async functions?

    <!-- language: lang-js -->
    // function to talk to Storage By Zapier to get a key
    //
    async function getStore(passKey) {
        try {
            const store = StoreClient(inputData.secret);
            const data = await store.get(passKey);
            console.log(`Here is the data from store: ${data}`);
            return data;
         } catch(error) {
            console.log(`We hit an error: ${error}`);
         }
     }

     // function to talk to Storage By Zapier to set a key and values
     //
     async function setStore(passKey, passTags) {
         try {
             const store = StoreClient(inputData.secret);
             await store.setMany({'email': passKey, 'tags': passTags});
         } catch(error) {
             console.log(`We hit an error: ${error}`);
         }
     }

     // for testing in editor
     //
     //var contactData = JSON.parse(raw);
     // only when Storage By Zapier not available 
     //var inputData  = JSON.parse(inputStore);

     // pull in data from Zapier
     //
     var contactData = JSON.parse(inputData.raw);
     //console.log(contactData);
     //console.log(inputData.secret);
     //var inputData = JSON.parse(inputData.testStore);
     //console.log(inputData.tags);
     //console.log(`inputData.tags: ${inputData.tags}`);

     //
     //  create output object to pass back to zapier after processing webhook from AgileCRM
     //
     //  receive raw webhook by parsing using built-in function
     //
     var output = new Object();

     //console.log(`inputData.tags: ${inputData.tags}`);

     var contactEventData, isValentus, isDoubleOptIn, isUpdate, contactTags, tagsDidntchange, storeTags, storeTagsArray, tagsEqualCount;

     // set-up variables and init
     //
     isValentus       = false;
     isDoubleOptIn    = false;
     tagsDidntchange  = false;
     contactTags      = new Array();
     storeTagsArray   = new Array();
     tagsEqualCount   = 0;
     contactEventData = contactData.eventData;

     //  find out is this is an update to a contact
     //
     contactData.eventName === 'Contact is Updated' ? isUpdate = true : isUpdate = false;

     //  find out if the contact is:
     //      Valentus, Double Opt-in and save other tags
     //
     contactEventData.tags.forEach(function(contactTag) {
         switch(contactTag) {
             case 'Valentus':
                 isValentus = true;
                 break;
             case 'Double_Opt_In':
                 isDoubleOptIn = true;
                 break;
             default:
                 contactTags.push(contactTag);
         }
     })

     //  initialize all variables to be passed via output object to Zapier
     //     ---this is so that the subsequent steps in the zap have data for the variable
     //
     var contactEmailAddress = '-', contactFirstName ='-', contactLastName = '-', contactPhoneNumber = '-', contactSource = '-', contactValentusUSERID = '-', contactStatus = '-', contactPhoneCallType = '-', contactInvitedby = '-', contactInvitedByEmail = '-', contactInvitedByWebsiteUID = '-', contactInvitedByValentusUSERID = '-', contactValentusTeam = '-';

     //  process contact properties to get needed variable data by property name
     //
     contactEventData.properties.forEach(function(properties) {
         let propertyName  = properties.name;
         let propertyValue = properties.value;
         switch(propertyName) {
             case 'email':
                 contactEmailAddress = propertyValue;
                 break;
             case 'first_name':
                 contactFirstName = propertyValue;
                 break;
             case 'last_name':
                 contactLastName = propertyValue;
                 break;
             case 'phone':
                 contactPhoneNumber = propertyValue;
                 break;
             case 'Source':
                 contactSource = propertyValue;
                 break;
             case 'Valentus userid': 
                 contactValentusUSERID = propertyValue;
                 break;
             case 'Status':
                 contactStatus = propertyValue;
                 break;
             case 'Phone call type':
                 contactPhoneCallType = propertyValue;
                 break;
             case 'Invited by':
                 contactInvitedby = propertyValue;
                 break;
             case 'Invited by email':
                 contactInvitedByEmail = propertyValue;
                 break;
             case 'Invited by website UID':
                 contactInvitedByWebsiteUID = propertyValue;
                 break;
             case 'Invited by Valentus userid':
                 contactInvitedByValentusUSERID = propertyValue;
                 break;
             case 'company':
                 contactValentusTeam = propertyValue;
                 break;
         }
     })

     //  get storage by email address and compare tags
     //  and see if tags changed.  if they changed, we don't
     //  want to process any further
     //
     //storeTags = inputData.tags;

     getStore(contactEmailAddress).then(data => {
         console.log(data);
         storeTags = data;
     });
     //console.log(`storeTags returned is: ${storeTags}`);

     //  Transform store string into array of tags to compare 
     //  with hook tags array
     //
     storeTagsArray = storeTags.split(',');

     //  compare storeTags to contactTags
     //    --note both arrays may not be in same order
     //
     contactTags.forEach(function(contactTag) {
         storeTagsArray.forEach(function(storeTag) {
             if (storeTag === contactTag) {
                 tagsEqualCount++;
             }
         })
     })

     if (tagsEqualCount === storeTagsArray.length && tagsEqualCount === contactTags.length) {
         tagsDidntchange = true;
     } else {
         setStore(contactEmailAddress, contactTags);
     }

     //  now place contact property variables into output object to pass
     //
     output = {
         'isValentus': isValentus,
         'isUpdate': isUpdate,
         'isDoubleOptIn': isDoubleOptIn,
         'tagsDidntchange': tagsDidntchange,
         'contactEmailAddress': contactEmailAddress, 
         'contactFirstName': contactFirstName, 
         'contactLastName': contactLastName, 
         'contactPhoneNumber': contactPhoneNumber, 
         'contactSource': contactSource, 
         'contactValentusUSERID': contactValentusUSERID, 
         'contactStatus': contactStatus, 
         'contactPhoneCallType': contactPhoneCallType, 
         'contactInvitedby': contactInvitedby, 
         'contactInvitedByEmail': contactInvitedByEmail, 
         'contactInvitedByWebsiteUID': contactInvitedByWebsiteUID, 
         'contactInvitedByValentusUSERID': contactInvitedByValentusUSERID, 
         'contactValentusTeam': contactValentusTeam,
         'contactTags': contactTags
     };

     /*  for debug */
     console.log(output);
     /**/

Comments