TypeError only when running the complete workflow

Hi. I have created a workflow, part of which grabs an image URL and looks to convert this to base64 so that it can be stored in a DB. The workflow element (updatePartImagesToBase64) that does the conversion works fine when I run it on its own, but when I run the whole workflow, I am getting an error of TypeError: fetch is not a function

[Fri Jan 20 2023 10:17:49 GMT+0000] Evaluating JS query: updatePartImagesToBase64
[Fri Jan 20 2023 10:17:49 GMT+0000] Error evaluating updatePartImagesToBase64: TypeError: fetch is not a function
[Fri Jan 20 2023 10:17:49 GMT+0000] --- Failed running query: updatePartImagesToBase64 ---
[Fri Jan 20 2023 10:17:49 GMT+0000] --- Hit failure, skipping queries: insertParts ---

The 'updatePartImagesToBase64' code is as follow, and loops through an array from the prior Workflow module (makeParts) using the .map function

const newPart = makeParts.data

const getBase64FromUrl = async (url) => {
  const data = await fetch(url);
  const blob = await data.blob();
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.readAsDataURL(blob); 
    reader.onloadend = () => {
      const base64data = reader.result;   
      resolve(base64data);
    }
  });
}

const base64Image = await Promise.all(makeParts.data.map(async (part, i) => {
        newPart[i].thumb = await getBase64FromUrl(part.thumb)
        newPart[i].thumb_120x120 = await getBase64FromUrl(part.thumb_120x120)
    }));

return newPart

I am no JavaScript expert, so I assume that there may be an issue with the code - but I am not sure why it would work when running on its own and then fail when the whole workflow is run.

Can anyone help me out please...?

1 Like

Hi @turps808! Happy to help here. When you manually click "Run" on the query, what gets returned?

Hi @victoria - many thanks for getting back me. When I run the query manually I get back a bunch of data rows as I would expect - and as part of that data, the image URLs have been successfully converted to Base64

Ah, thank you for clarifying!

I double checked with the engineers that build Workflows and learned:

Manually running happens on the client side, and running in full/on the schedule happens on the server side.

In this case, fetch is likely in scope on the client side JS, but not available when it's run on the server.

We're working on making everything server side soon to reduce inconsistencies like this!

Thanks for following that up @victoria.
Is there a feature request or similar that I can follow so that I know when this has been updated..?

Great question. I linked this thread to the internal feature request, so I'll be sharing updates directly in this thread!

Hi @victoria any update on this? We get a lot of these errors as well.

Checking with eng now :slight_smile:

Hi,
I am also facing a similar issue where the error is 'fetch' is not defined. When I try to import it like a regular js lib, it is unable to find the module as well.

This is in the workflows specifically.

Are libraries not working in workflows?

Hey Haseeb! We currently don't support fetch in Workflows, but we have an open request to support this :slight_smile:

Custom libraries in Workflows isn't supported yet, but it's coming soon (hopefully by the end of this quarter!)

Any update on this?

Hi @Etuzy Thanks for checking in! This should be doable now :slightly_smiling_face:

Do you see this option in your Workflow?