Error in Javascript column

Hi all

I have the below Javascript which is actually working nicely - but I get an error message within the dialogue box. Am I safe to ignore it?

The script takes a long JSON string, and filters dynamically on specific tags - which are then sent to a PDF generator script.

return (function(p1, p2) {
  const json = JSON.parse(p1);
  const includeTags = p2.split(",").map(t => t.trim());

 const filteredDates = json.dates
    .map(day => ({
      ...day,
      detail: day.detail.filter(entry =>
        Array.isArray(entry.data?.tags) &&
        entry.data.tags.some(tag => includeTags.includes(tag))
      )
    }))
    .filter(day => day.detail.length > 0);

 const result = {
    ...json,
    dates: filteredDates
  };

 return JSON.stringify(result);
})(p1, p2);

Thanks in advance.

Andrew

PS - Not much of a coder - as you can probably tell, CHatGPT helped me out!

You will see that error if any rows are empty for p2.
You could code around the error if you want, but it’s safe to ignore it.

2 Likes

A suggestion, you can also add console.log(“some variable”); to help you debug.
And also take some time to learn Javascript, it’s both super handy and nowadays, you can code everything with it. For example, if you want to code a fullstack JS app with Vue or Next.

1 Like

To prevent the JS Column from giving errors when p2 is empty, you could add:
if(!p2) return;

2 Likes

Excellent suggestion of MaximeBaker,
In JS, if a data is empty it doesn’t exist. Thus if(!p2) = empty = code will stop.
Again, learn JS guys and girls, it’s top language :smiley:

1 Like

Good advice

This looked non sensical to me

p2.split(“,”).map(t => t.trim());

But now I’ve worked out what map and => does - it’s genius!

1 Like

I am glad it worked!

I’m intrigued.

How / when does the JavaScript execute? I assume it’s server side? Or is it? Does it somehow execute in the browser?

Apologies if that’s a dumb question!

Yes, the JavaScript executes client-side (in the browser). This is why we always warn against using JavaScript to make authenticated API calls.

The same is true for the Experimental Code column. Even though you might be hosting the code on GitHub, the code is first loaded onto the client device and then executed locally.

As far as when it is executed, my understanding is that it’s the same as all other computed columns - that is, the JavaScript will execute whenever the column is referenced. Although there might be some local caching involved - I’m not sure about that.

1 Like

@Darren_Murphy

As far as when it is executed, my understanding is that it’s the same as all other computed columns - that is, the JavaScript will execute whenever the column is referenced. Although there might be some local caching involved - I’m not sure about that.

Nope Darren, “experimental” JS column tables are always executed quite fast for me. But maybe because my Glide tables are below <20k records. There is a mix of cache and in RTDB in Glide Tables. Who uses Firestore as a data storage in backend. A very good tech if you ask me.

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