Finish code workflow n8n

Describe the problem/error/question

I’m taking a course and the video doesn’t show all the code, and I’m almost done. Help please! (Code x Airtable)

The objective of this workflow is to delete the corresponding client from the Airtable table corresponding to the client who wants to cancel it, the code has to identify which one of those that arrive

The guy in the video forgets to show all the code


What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

{
“nodes”: [
{
“parameters”: {
“jsCode”: “// Paso 1: Obtener los datos del cliente desde el nodo ‘Edit Fields’\nconst cliente = $(‘Edit Fields’).first().json;\n\nconst horaCliente = cliente.Hora?.trim();\nconst fechaCliente = cliente.Fecha?.trim();\nconst nombreCliente = cliente.Nombre?.trim().toLowerCase();\nconst telCliente = cliente.Telefono?.trim();\n\n// Paso 2: Obtener todos los registros de Airtable\nconst registros = $input.all();\n\n// Paso 3: Filtrar registros que coincidan con los 4 campos\nconst coincidencias = registros.filter(item => {\n const fields = item.json.fields || {};\n\n const hora = fields.Hora?.trim();\n const fecha = fields.Fecha?.trim();\n const nombre = fields.Nombre?.trim().toLowerCase();\n const tel = fields.Telefono?.trim();\n\n return (\n hora === horaCliente &&\n fecha === fechaCliente &&\n nombre === nombreCliente &&\n tel === telCliente\n );\n});\n\n// Paso 4: Devolver coincidencia o mensaje de error\nif (coincidencias.length === 0) {\n return [{\n json: {\n resultado: “No se encontró ninguna reserva con esos datos para cancelar.”\n }\n }];\n}\n\nreturn coincidencias;\n”
},
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
64,
16
],
“id”: “7870137a-3cd4-46b7-8dd2-518a2263ddd2”,
“name”: “Code in JavaScript”
}
],
“connections”: {
“Code in JavaScript”: {
“main”: [

]
}
},
“pinData”: {},
“meta”: {
“templateCredsSetupCompleted”: true,
“instanceId”: “f60311e5337308d0f8c1efc4eb99285eaa09f21ca35ef2312c7c2dbb6e8d80b9”
}
}

I did a quick pass at it with AI, can you check if this will output what you need?

// Paso 1: Obtener los datos del cliente desde el nodo 'Edit Fields'
const cliente = $("Edit Fields").first().json;
const horaCliente = cliente.Hora?.trim();
const fechaCliente = cliente.Fecha?.trim();
const nombreCliente = cliente.Nombre?.trim().toLowerCase();
const telCliente = cliente.Telefono?.trim();

// Paso 2: Obtener todos los registros de Airtable (input viene del nodo Airtable)
const registros = $input.all();

// Paso 3: Filtrar registros que coincidan con los 4 campos
const coincidencias = registros.filter((item) => {
  const fields = item.json.fields || {};
  const hora = fields.Hora?.trim();
  const fecha = fields.Fecha?.trim();
  const nombre = fields.Nombre?.trim().toLowerCase();
  const tel = fields.Telefono?.trim();

  return (
    hora === horaCliente &&
    fecha === fechaCliente &&
    nombre === nombreCliente &&
    tel === telCliente
  );
});

// Paso 4: Devolver coincidencia(s) o mensaje de error
if (coincidencias.length === 0) {
  return [
    {
      json: {
        resultado: "No se encontró ninguna reserva con esos datos para cancelar.",
      },
    },
  ];
}

// Paso 5: Mapear coincidencias al formato requerido
return coincidencias.map((item) => {
  const { id, createdTime, fields } = item.json;
  return {
    json: {
      id,
      createdTime,
      fields: {
        hora: fields.Hora || null,
        fecha: fields.Fecha || null,
        notas: fields.Notas || null,
        nombre: fields.Nombre || null,
        tel: fields.Telefono || null,
        tipo_de_mesa: fields.Tipo_de_mesa || null,
        numero_personas: fields.Numero_personas || null,
        disponibillidad: fields.Disponibillidad || null,
      },
    },
  };
});

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