Best answer

Trigger Custom Code Error: Invalid API Response: - Results must be an array, got: undefined, (undefined)

  • 26 April 2023
  • 4 replies
  • 202 views

Userlevel 1
Badge

Hi! I’m looking for some assistance in troubleshooting the custom code I’m trying to write for a trigger I’m setting up. I’ve referenced the Zapier documentation and other posts similar to the situation here, here, and here. The solution for this situation appears to be wrapping the response as an array “[]”, however, that doesn’t seem to be working in my situation. Below is my code and error response. 

 

In the documentation for the API I’m trying to work with, the example request looks like this:

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

fetch('https://api.portal.scanifly.com/api/v1/designs/63fbc6498b05fa000747513b?access_token=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));

When I run this code on npm.runkit.com/supertest I get the following results:

 

Any thoughts on what I’m missing? Thanks!

icon

Best answer by sgee 16 June 2023, 18:15

View original

4 replies

Userlevel 7
Badge +11

Hey @sgee,

I’m going to move this topic into the Developer Discussion forum, where some of our more technical members hang out. That should get some more eyeballs on it :) 

Userlevel 1
Badge

Thanks @nicksimard. I appreciate it!

Userlevel 4
Badge +9

Hey @sgee 👋

I see you’ve discussed this action with a few members of our Support team - great! 

To recap for the Community - a search is indeed expecting an array of JSON objects and often this can be achieved by wrapping the response as an array, as you tried.

Since you were then receiving an ‘undefined’ error, I’d recommend checking the Monitoring tab, to see what response the GET is returning from that endpoint, before you wrap it in an array. 

Checking the API docs you’ve linked, I also noticed that the projectId is expected in the url path, so in your screenshot, you’d want to edit the url to be

`https://api.portal.scanifly.com/api/v1/designs/${bundle.inputData.projectId}` 

when using Code Mode.  if you haven’t tried that already 😀

Userlevel 1
Badge

Hi @MarinaH, I forgot this question was still open.

Yes, you’re correct about needing to have `projectId` in the URL path. The error message I was receiving was a bit of a red herring.

Below is the correct custom code for my situation.

  
const options = {
url: `https://api.portal.scanifly.com/api/v1/designs/${bundle.inputData.projectId}`,
method: 'GET',
headers: {
'X-ACCESS-TOKEN': bundle.authData.access_token
},
params: {
'access_token': bundle.authData.access_token
}
}

return z.request(options)
.then((response) => {
response.throwForStatus();
const results = response.json;

// You can do any parsing you need for results here before returning them

return results;
});

 

Reply