Avoid IF node with better JS in Code node?

I have this function that filters the items from an HTTP node and only returns records created in the last X hours.

const allItems = items[0].json.map(i=>({json:i}));
const filteredItems = allItems.filter(i=>{
  const createdIso = i?.json?.created_iso;
  const xHrsAgo = new Date(
    Date.now() - 4 * 60 * 60 * 1000,
  ).toISOString();
  return createdIso > xHrsAgo;
});
return {filteredItems}

It’s working, but I have to add an IF node afterwards to stop the workflow if there are no items after filtering. It seems like there should be an easier way to do this right in the Code block, so it stops the workflow there, without needing the separate IF block to check the array length.

I tried a few other things but got errors about not returning an array of items. Do you always have to return an array of items for the code to be valid, then use another block to decide what to do with it?

Hi @joseph_appsmith :wave: Welcome to the community :tada:

Could you share some example JSON of what you’re working with from the HTTP Request node? With some sample data I might be able to dig into this further for you :slight_smile: I can’t promise I can make this happen without the If node but I’d be happy to give it a shot :smiley:

Thanks @EmeraldHerald ! Appreciate the help. Here’s a sample of the data:

[
  {
    "id": "1101",
    "link": "https://community.appsmith.com/content/video/thursday-livestream-appsmith-innovate",
    "type": "Video",
    "title": "Thursday Livestream : Appsmith Innovate!",
    "summary": "Thursday, October 19\n10:00 AM to 12:00 PM EDT\nSign up here: https://lu.ma/elyne08s\n📅 Join us for a whirlwind virtual conference dedicated entirely to Appsmith. In just two hours, we'll be packing in a series of hyper-focused 15-minute sessions, each one designed to maximize information while respecting your time.",
    "image": "",
    "tags": "virtual event",
    "author": "youtubeapi",
    "author_roles": "Staff, Site builder",
    "created": "2023-10-18",
    "created_timestamp": "1697625477",
    "created_iso": "2023-10-18T10:37:57+00:00"
  },
  {
    "id": "1098",
    "link": "https://community.appsmith.com/tutorial/how-set-google-calendar-datasource-appsmith",
    "type": "Tutorial",
    "title": "How To Set Up a Google Calendar Datasource in Appsmith",
    "summary": "In this short tutorial, I'd show you how to configure your google calendar API, and set it up on your Appsmith instance as an authenticated data source. \nWant to skip straight to the template? Check it out here! ",
    "image": "https://community.appsmith.com/sites/default/files/styles/card_image/public/micah-williams-lmFJOx7hPc4-unsplash.jpg?itok=vN1roARX",
    "tags": "Google, Calendar, Google Cloud Platform, Datasource",
    "author": "jacquesikot",
    "author_roles": "Community, Author, Staff, Site builder",
    "created": "2023-10-17",
    "created_timestamp": "1697514598",
    "created_iso": "2023-10-17T03:49:58+00:00"
  },
  {
    "id": "1097",
    "link": "https://community.appsmith.com/tutorial/zendesk-api-creating-authenticated-api-datasource",
    "type": "Tutorial",
    "title": "Zendesk API: Creating an Authenticated API Datasource",
    "summary": "The Zendesk API has options to connect via user name and password, or by using an API token. This tutorial will be using the more secure, token method, and will show how to create an Authenticated API Datasource in Appsmith, to securely store the token on the Appsmith server.",
    "image": "https://community.appsmith.com/sites/default/files/styles/card_image/public/Copy%20of%20Community%20Portal%20Template%20%281%29.png?itok=z_i6tYYD",
    "tags": "zendesk, APIs, datasources, authentication, Support",
    "author": "joseph_appsmith",
    "author_roles": "Staff, Admin",
    "created": "2023-10-14",
    "created_timestamp": "1697283113",
    "created_iso": "2023-10-14T11:31:53+00:00"
  }
]
1 Like

Hi @joseph_appsmith :wave: I can poke at this more later as well, but for now, I think that the If node might be your best bet :bowing_man:

1 Like

Thanks for taking a look @EmeraldHerald. Just wanted to make sure I wasn’t missing something simple here.

So, in general, there’s no way for a Code node to return a value that stops the workflow? It would be great if you could use a try/catch, and terminate the workflow in the catch block if the error was expected.

Hey @joseph_appsmith,

What about something like the below? So if there are no items returning null would stop the code node at that point as it won’t have any items to output.

if (filteredItems.length > 0) {
  return {filteredItems}
}
return null
2 Likes

Thanks @Jon ! This is what I was looking for. It’s stopping the workflow and there’s no error about the output type now.

I would actually have expected that instead of:

return {filteredItems};

you should do:

return filteredItems;

Because that changes two things:

  1. It returns again different items instead of all items merged together into one with a json property
  2. It will also ensure that it simply stops if there is no data

To be clear, the simplified code would then look like this:

const allItems = items[0].json.map(i=>({json:i}));
return allItems.filter(i=>{
  const createdIso = i?.json?.created_iso;
  const xHrsAgo = new Date(
    Date.now() - 4 * 60 * 60 * 1000,
  ).toISOString();
  return createdIso > xHrsAgo;
});
2 Likes

Even better! Thanks @jan !

2 Likes

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