HOW-TO

How to stop n8n Google Calendar errors passing as empty results

Published · Updated

Code

js
// n8n Code node, mode: Run Once for All Items.
// Place it right after Google Calendar (Get Many) with
// Always Output Data: ON and On Error: Continue (regular output).
function classifyCalendarLookup(items) {
  const calendarEvents = [];
  let errorMessage = '';

  for (const item of items) {
    const json = (item && item.json) || {};
    const failure = json.error;

    if (failure) {
      // Error item produced by "Continue (regular output)".
      errorMessage = typeof failure === 'string'
        ? failure
        : String(failure.message || 'Calendar lookup failed');
      continue;
    }

    // Placeholder item produced by Always Output Data: no keys at all.
    if (Object.keys(json).length === 0) continue;

    calendarEvents.push(json);
  }

  const lookupFailed = errorMessage !== '';

  return [{
    json: {
      lookupFailed,
      errorMessage,
      eventCount: lookupFailed ? 0 : calendarEvents.length,
      calendarEvents: lookupFailed ? [] : calendarEvents,
    },
  }];
}

// Last line inside the n8n Code node (remove the two slashes):
// return classifyCalendarLookup($input.all());

Why it happens

The two settings do separate jobs and do not know about each other. With the error output, n8n moves the failed item to the error branch, which leaves the main branch empty. Always Output Data then sees an empty main branch and adds one blank item, so the main branch runs too. A blank item looks exactly like "no events found", so the booking path treats a failed lookup as a free slot.

Steps

  1. Open the Google Calendar node settings. Keep Always Output Data on. Change On Error to "Continue (regular output)".
  2. Add a Code node directly after it, in "Run Once for All Items" mode. Paste the code and uncomment the last line.
  3. The code reads every incoming item. An item with an error field marks the lookup as failed.
  4. A blank item is skipped. Every other item is kept as a calendar event.
  5. The node returns one item with lookupFailed, errorMessage, eventCount and calendarEvents.
  6. Add an IF node on {{ $json.lookupFailed }}. The true branch sends the failure response. The false branch goes to your conflict check, which reads calendarEvents.

Pitfalls

  • The check relies on the error field that n8n adds when a node continues after a failure. Test it on your own n8n version with an invalid Calendar ID before going live.
  • Leave the error output unconnected or switch it off. If both modes are mixed, the blank item comes back.
  • The Code node must run once for all items. In "Run Once for Each Item" mode it cannot see the whole result.
  • A failed lookup must never mean "slot is free". The code returns an empty event list on failure, so always route on lookupFailed first.
  • For short outages, also turn on Retry On Fail in the Calendar node, so a single timeout does not reject a booking.

Seen on: n8n Community ·

Related service: Python automation and data collection