Skip to main content
Best answer

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


sgee
Forum|alt.badge.img
  • Beginner
  • 7 replies

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!

Best answer by sgeeBest answer by sgee

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;
  });

 

View original
Did this topic help you find an answer to your question?

4 replies

nicksimard
Forum|alt.badge.img+11
  • Zapier Staff
  • 2115 replies
  • April 25, 2023

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 :) 


sgee
Forum|alt.badge.img
  • Author
  • Beginner
  • 7 replies
  • April 26, 2023

Thanks @nicksimard. I appreciate it!


Forum|alt.badge.img+9
  • Zapier Staff
  • 238 replies
  • June 14, 2023

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 😀


sgee
Forum|alt.badge.img
  • Author
  • Beginner
  • 7 replies
  • Answer
  • June 16, 2023

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;
  });