Best answer

ReferenceError: fetch is not defined

  • 27 April 2023
  • 8 replies
  • 1291 views

Userlevel 1
Badge

Hi! I’m building an Action and trying to use `fetch` via code mode. However, when doing so I receive a “ReferenceError: fetch is not defined”. In doing some research, it appears that Node.js needs to be v18 in order to use this. Is this correct? Is there a workaround?

const options = {method: 'GET', headers: {accept: 'application/json'}};

fetch('https://api.portal.scanifly.com/api/v1/designs/', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

 

icon

Best answer by sgee 27 April 2023, 22:47

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.

8 replies

Userlevel 6
Badge +8

Try this:

const Project_Id = ""; // Include your own project ID
const Access_Token = ""; // Include your own access token
let appData = "";

try {
const appResp = await fetch(
`https://api.portal.scanifly.com/api/v1/designs/${Project_Id}?access_token=${Access_Token}`,
{
method: 'GET', // Method
headers: {
'accept': 'application/json'
}
}
);

appData = await appResp.text();
appData = JSON.parse(appData);
} catch (e) {
console.log(e.message);
}

output = [{ appData }]

 

Userlevel 1
Badge

@Todd Harper thank you for your reply! I tried out your code and received the following error response.

When I ran this is npm.runkit.com/supertest I got the following result.

 

Userlevel 6
Badge +8

Hi @sgee!

Huh. It clearly returns an array in your npm.runkit.com test, so I’m surprised that the error message you’re seeing says an array was not returned.

Are you running this test from within the Zapier editor? Can you screenshot the entirety of the “Action” and “Test” steps (of course, please obfuscate or redact your access token).

Userlevel 1
Badge

Hi @Todd Harper,

 

Yes, I’m running this test from within the Zapier editor. Ultimately, I’d like to get to where I can use the `params` to pass the projectId and access_token, but I just hardcoded my projectId and access_token to test the code you sent over. Here are the screenshots.

params: {
'projectId': bundle.inputData.projectId,
'access_token': bundle.authData.access_token
}

 

Let me know if you need anything else!

Userlevel 6
Badge +8

Oh! @sgee, you are using the developer platform, not a Code by Zapier step. That makes more sense given the level of detail your error message provided.

I don’t have an answer for you, unfortunately, but you may want to consider reposting this in the Developer Zone (or perhaps ask the admins to move it) to get more opinions specific to the Zapier Developer Platform (https://platform.zapier.com/), which I actually have very little experience using.

The “Code & Webhooks” area is technically reserved for questions about the Code by Zapier and Webhooks by Zapier apps within the user-facing side of Zapier (https://zapier.com/).

Userlevel 1
Badge

@Todd Harper shoot! I thought that’s what I was doing, but it looks like I didn’t get the right forum category selected 🤦 Sorry for wasting your time. I appreciate the help you’ve given me so far, tho!

Userlevel 6
Badge +8

@sgee Not a waste of time at all!! I still think the code I provided here will work for you, but there may just be some minor modifications needed to make it work in Zapier Platform. People over in that forum have some really great insights, so I’m confident you’ll get the issue resolved :)

Userlevel 1
Badge

Crosspost of the post in the Developer Discussion forum: