Javascript Can';无法确定JS storeClient函数的问题和引发的错误

Javascript Can';无法确定JS storeClient函数的问题和引发的错误,javascript,zapier,Javascript,Zapier,我在JavaScript Zapier步骤中创建了两个异步函数来解析原始webhook。这两个异步函数以storeClient为中心,通过Zapier在存储器上“获取”和“设置”值。但是,我抛出了一个错误。我有Zapier测试子步骤的屏幕截图。由于Zapier在出错时返回的方式,我看不到任何console.log输出 我对异步函数做了什么错误 // function to talk to Storage By Zapier to get a key // async fu

我在JavaScript Zapier步骤中创建了两个异步函数来解析原始webhook。这两个异步函数以storeClient为中心,通过Zapier在存储器上“获取”和“设置”值。但是,我抛出了一个错误。我有Zapier测试子步骤的屏幕截图。由于Zapier在出错时返回的方式,我看不到任何console.log输出

我对异步函数做了什么错误

    // 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);
     /**/

这里是Zapier平台团队的David

问题的根源在以下行:

 storeTagsArray = storeTags.split(',');
如果
storeTagsArray
undefined
,则对其调用
.split()
会崩溃

它未定义的原因是您处理上述异步代码的方式。也就是说,这个,就在上面:

getStore(contactEmailAddress).then(data => { // line 1
     console.log(data);                      // line 2
     storeTags = data;                       // line 3
 });

storeTagsArray = storeTags.split(',');       // line 4
在承诺之后使用
.then
是正确的,但是
then
之外的代码在此期间继续运行。上面的执行顺序是第1行,第4行,(最终)第2行,第3行。由于4在
storeTagsArray
初始化之前运行,因此它仍然
未定义

这里的解决方案很简单-使用
等待

storeTags = await getStore(contactEmailAddress)
storeTagsArray = storeTags.split(',');      
我们将您的所有代码封装在一个大的
async
函数中,这样您就可以在任何需要的地方等待它了。还有更多的信息


您还有另一个
。然后需要以类似的方式修复
,然后您的代码应该自上而下运行,正如预期的那样

欢迎来到StackOverflow。尽量明确你的问题是什么,避免一次问一个以上的问题,这样你的帖子才会引起注意,否则人们就会飞走。没有人喜欢阅读更多的文本。这看起来像一个很棒的用例!我认为您遇到了一些异步问题,代码运行出现了问题。也就是说,您的
。然后
不会在其余的运行之前完成。你能用错误截图更新你的帖子吗?这样我就可以确认了@xavdid,我知道了。请参阅附件中的文章。您确实找到了答案,还是仍然需要帮助?我想我已经发现了你的错误不,我还没有找到问题所在。我相信只要你告诉我,这将是一个呼喊的时刻。非常感谢@xavdid。我知道这一定是我忽略了的简单的事情。我很快就会试试。再次感谢。