Best answer

TypeError: Cannot read property 'length' of undefined

  • 31 October 2023
  • 12 replies
  • 603 views

Hello - receiving this error on my zap that was formerly working perfecting for months.

 

It’s throwing the error in the step “Run javascript in Code by Zapier”. I get the error of “TypeError: Cannot read property 'length' of undefined”. 

The goal of the zap is to send an email via Outlook which it does not, but it’s also supposed to update the Smartsheet so that you know it sent the email. It’s getting to that step despite having a error in an earlier step and not actually sending an email via outlook.

icon

Best answer by Troy Tessalone 1 November 2023, 23:25

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

12 replies

Userlevel 6
Badge +8

Can you please post screenshots of your code step, including the input data that is being used and the code itself? And more specifically, it would be vital to know the type of the data you’re trying to get the length of (string, array, etc.)

Userlevel 7
Badge +14

Hi @Nate. E 

Good question.

Can you post screenshots with how your Zap steps are configured in detail?

We would need to see the Zap code and inputs to better understand what might be causing the error. (screenshots)

The Code error means an expected variable value is likely missing.

You can try asking ChatGPT for help with troubleshooting the Code.

So I did end up using the beta “Generate with AI” tool within the step to fix this. I simply copied the current code in the Javascript step into the AI tool and ran a test. Thankfully it kicked out the same error, and then offered to fix it. The fix worked and the zap starting working again. Still haven’t figured out how it was completing “lower” steps when it wasn’t getting through that one, but at least it works now. 

 

Red is the step that failed, green is the step that did the action in the Smartsheet which is adding a timestamp to a cell, yellow is the date/time formatter which may be why it did the lower green step anyway??


 

This post has been edited by a moderator to remove personal information. Please remember that this is a public forum and to remove any sensitive information prior to posting.

Userlevel 7
Badge +14

@Nate. E 

FYI: Zap steps not depending on output from a previous Zap step will try to run, unless you add a Filter step to prevent those from proceeding to process.

Thanks Tony - So another “only continue if” step just before Step 12 that would reference step 11? So “only continue if step 11 was completed?

Userlevel 7
Badge +14

@Nate. E 

Perhaps simply update the Path filter conditions to only continue if there is an expected value output from the Code step.

@Todd Harper

@Troy Tessalone

Hey Todd/Troy - ok, while the AI fix did get the emails sending again I realized it stopped pulling the attachment from the row in SS. So I reverted back to the original code to just work on the original problem of “TypeError: Cannot read property 'length' of undefined”.

 

I tried using ChatGPT for a while as well as re-using the zapier AI, but nothing seems to work. In the version of the zap that sent an email but did not pull the attachment...the “document” within step 11 (send email via outlook) was yellow but I wasn’t sure how to reselect, so I assume it’s just not coded correctly in the javascript step. 

So here is the original code, and screen shots of the steps:

 

 

 

Steps:

 

Here is the code that the zapier AI came up with that seemed to work but failed to include the attachment:

// Define an object with a property named 'document'var obj = {  document: "",};// Construct the URL for the API request using the row ID from the inputDatavar url = `https://api.smartsheet.com/2.0/sheets/6322227864987524/rows/${inputData.row}/attachments`;// Make a GET request to the constructed URLvar res = await fetch(url, {  method: "GET",  headers: {    // Include the authorization token in the request headers    Authorization: "Bearer XXXXXXXXXXXXXXXXXXXX",  },});// Parse the response body as JSONvar body = await res.json();// Check if the response data contains any attachmentsif (body && body.data && body.data.length > 0) {  // If attachments are found, construct the URL for the attachment request using the attachment ID from the response  var url = `https://api.smartsheet.com/2.0/sheets/6322227864987524/attachments/${body.data[0].id}`;  // Make a GET request to the constructed URL  var res1 = await fetch(url, {    method: "GET",    headers: {      // Include the authorization token in the request headers      Authorization: "Bearer XXXXXXXXXXXXXXXXXXXX",    },  });  // Parse the response body as JSON  var body1 = await res1.json();  // Update the 'document' property of the 'obj' object with the URL of the attachment  obj.document = body1.url;}// Return the 'obj' objectreturn obj;

This post has been edited by a moderator to remove personal information. Please remember that this is a public forum and to remove any sensitive information prior to posting.

Userlevel 7
Badge +14

@Nate. E 

To help you troubleshoot, check the Zap Runs DATA IN/OUT for each step.

Then you can recreate the specific request to try to replicate the error.

https://zapier.com/app/history/

@Troy Tessalone 

ok thanks Troy, would this be what we’re looking for? Says the “document” field contains no value. But there was an attachment.

 

 

Userlevel 6
Badge +8

@Nate. E,

Assuming both of the API tokens, the sheet ID, and the row ID in your inputData is correct, I have a couple of thoughts and questions:

  1. Are you sure there is an attachment in the row you have test data for? I’m debugging your code one step at a time and noticed that after your first API call (where you are simply checking for a list of attachments), you are getting a “Not Found” error, which means that your “body” variable does not get assigned a value. Because of this, the AI’s “Check if the response data contains any attachments” conditional statement is being skipped, ensuring that your “obj” object remains empty.
  2. Is there a reason you are using two different API keys instead of one?
  3. A word of caution: it’s not safe to publicly publish your API keys online. While Troy and I are good guys, someone out there might take your API key and run an endless loop on your spreadsheet, populating it with unreadable gobbledegook and nonsense (or worse, malicious code). If those are real API keys (and I believe they are, since I was able to run your code without getting an authorization error), please edit your earlier comments and redact or delete any screenshots or code where the API keys are revealed.

@Todd Harper 

 

Yes, there is definitely an attachment, and I just ran a test through the Smartsheet again to be sure. The email arrives correctly, but there is no attachment.

Thanks for your advice about the API keys. I’m playing catch up here on an previously existing zapier account and zaps, so I’m not sure what I’m doing. It was all working correctly for many months. How do I edit a post? I’m not seeing any option to? I put a support ticket into zapier to get them redacted.

 

Userlevel 6
Badge +8

@Nate. E 

That’s pretty odd! I’d be really interested in taking a look at this with you synchronously so I can see it in better detail rather than relying on screenshots alone. Just DMed you and we can set up a meeting to chat about it.

As for redacting your keys - you can edit your own comments by hovering over the three dots in the lower right corner of the comment and then clicking on “Edit” (see screenshot).