Skip to content

Commit

Permalink
fix(RSS Feed Trigger Node): Handle empty items gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Sep 17, 2024
1 parent 6a35812 commit 6709503
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/nodes-base/nodes/RssFeedRead/RssFeedReadTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { NodeConnectionType, NodeOperationError } from 'n8n-workflow';
import Parser from 'rss-parser';
import moment from 'moment-timezone';

interface PollData {
lastItemDate?: string;
lastTimeChecked?: string;
}

export class RssFeedReadTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'RSS Feed Trigger',
Expand Down Expand Up @@ -39,12 +44,12 @@ export class RssFeedReadTrigger implements INodeType {
};

async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const pollData = this.getWorkflowStaticData('node');
const pollData = this.getWorkflowStaticData('node') as PollData;
const feedUrl = this.getNodeParameter('feedUrl') as string;

const now = moment().utc().format();
const dateToCheck =
(pollData.lastItemDate as string) || (pollData.lastTimeChecked as string) || now;
const dateToCheck = Date.parse(
pollData.lastItemDate ?? pollData.lastTimeChecked ?? moment().utc().format(),
);

if (!feedUrl) {
throw new NodeOperationError(this.getNode(), 'The parameter "URL" has to be set!');
Expand Down Expand Up @@ -73,14 +78,15 @@ export class RssFeedReadTrigger implements INodeType {
return [this.helpers.returnJsonArray(feed.items[0])];
}
feed.items.forEach((item) => {
if (Date.parse(item.isoDate as string) > Date.parse(dateToCheck)) {
if (Date.parse(item.isoDate as string) > dateToCheck) {
returnData.push(item);
}
});
const maxIsoDate = feed.items.reduce((a, b) =>
new Date(a.isoDate as string) > new Date(b.isoDate as string) ? a : b,
).isoDate;
pollData.lastItemDate = maxIsoDate;

if (feed.items.length) {
const maxIsoDate = Math.max(...feed.items.map(({ isoDate }) => Date.parse(isoDate!)));
pollData.lastItemDate = new Date(maxIsoDate).toISOString();
}
}

if (Array.isArray(returnData) && returnData.length !== 0) {
Expand Down

0 comments on commit 6709503

Please sign in to comment.