Help my code Isn't working

The code does not work, the code should select true in this case because the data is correct

// 1. Datos del cliente desde el nodo “Edit Fields”
const cliente = $items(“Edit Fields”)[0].json;
const horaCliente = cliente.Hora?.trim();
const fechaCliente = cliente.Fecha?.trim();
const nombreCliente = cliente.Nombre?.trim().toLowerCase();

// 2. Todos los registros del nodo “Search Records”
const reservas = $items(“Search records”).map(item => item.json);

// 3. Buscar coincidencia flexible
// - Coincide siempre por Nombre + Fecha
// - Si el cliente dio una hora, también la comprobamos
const match = reservas.find(r =>
r.Fecha?.trim() === fechaCliente &&
(r.Nombre || “”).trim().toLowerCase() === nombreCliente &&
(!horaCliente || r.Hora?.trim() === horaCliente)
);

// 4. Devolver resultado
if (match) {
return [{
json: {
encontrado: true,
mensaje: Reserva encontrada para ${match.Nombre} el ${match.Fecha} a las ${match.Hora}.,
fields: match
}
}];
} else {
// Opcional: mostrar sugerencias si coincide Nombre + Fecha aunque no coincida la hora
const sugerencias = reservas.filter(r =>
r.Fecha?.trim() === fechaCliente &&
(r.Nombre || “”).trim().toLowerCase() === nombreCliente
);

if (sugerencias.length > 0) {
return [{
json: {
encontrado: false,
mensaje: No se encontró reserva con esa hora, pero sí para ${cliente.Nombre} el ${fechaCliente}. Horas disponibles: ${sugerencias.map(s => s.Hora).join(", ")},
sugerencias,
cliente
}
}];
}

return [{
json: {
encontrado: false,
mensaje: “No existe ninguna reserva con esos datos”,
cliente
}
}];
}

You literally hardcoded false in the return:

Ah never mind, there is more than one return

Could you share the input? Pin the input and share it as JSON. All 10 items.
Or pin the input to the code node and share the workflow.

I’d prefer to see the whole workflow, please and please please pin the input data, it is very important. Both Search and Edit nodes.

This?
[
{
“id”: “rec82SIDbH9CjKphy”,
“createdTime”: “2025-09-16T18:04:19.000Z”,
“Hora”: “19:00”,
“Tipo_de_mesa”: “Interior”,
“Numero_de_personas”: 0,
“Disponibilidad”: “Disponible”
},
{
“id”: “recBV2aEoLZ4IJUEx”,
“createdTime”: “2025-09-09T14:40:50.000Z”,
“Hora”: “11:30”,
“Tipo_de_mesa”: “Terraza”,
“Disponibilidad”: “Disponible”
},
{
“id”: “recISJeIe53N3hFfx”,
“createdTime”: “2025-09-09T14:40:50.000Z”,
“Hora”: “20:00”,
“Tipo_de_mesa”: “Interior”,
“Disponibilidad”: “Disponible”
},
{
“id”: “recKx4fpIItQtuGvY”,
“createdTime”: “2025-09-09T14:40:50.000Z”,
“Hora”: “14:00”,
“Tipo_de_mesa”: “Terraza”,
“Disponibilidad”: “Reservada”
},
{
“id”: “recfkeSWrj40P97lr”,
“createdTime”: “2025-09-20T18:47:38.000Z”
},
{
“id”: “recg9PoWNQnjEQJ86”,
“createdTime”: “2025-09-11T14:32:53.000Z”,
“Hora”: “16:30”,
“Tipo_de_mesa”: “Interior”,
“Disponibilidad”: “Reservada”
},
{
“id”: “recgc7jCjPouJepWt”,
“createdTime”: “2025-09-09T14:40:50.000Z”,
“Hora”: “19:00”,
“Tipo_de_mesa”: “Interior”,
“Disponibilidad”: “Disponible”
},
{
“id”: “reciTy0LEye5uFfaD”,
“createdTime”: “2025-09-16T18:42:54.000Z”,
“Hora”: “19:30”,
“Fecha”: “2025-09-25”,
“Nombre”: “Eric”,
“Telefono”: “640630459”,
“Tipo_de_mesa”: “Interior”,
“Numero_de_personas”: 2,
“Disponibilidad”: “Reservada”
},
{
“id”: “recqbBiLU6SKwAXAr”,
“createdTime”: “2025-09-09T14:40:50.000Z”,
“Hora”: “13:30”,
“Tipo_de_mesa”: “Terraza”,
“Disponibilidad”: “Reservada”
},
{
“id”: “reczoLtUpTAc3O4wS”,
“createdTime”: “2025-09-09T14:40:50.000Z”,
“Hora”: “21:15”,
“Tipo_de_mesa”: “Interior”,
“Disponibilidad”: “Disponible”
}
]

And the content of the Edit Fields, please

And the code from the Code node, only properly formatted, please.

Use </> button to format the code.

{
“nodes”: [
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“id”: “07091f06-c575-4be0-823f-26449c8f5cc8”,
“name”: “Hora”,
“value”: “={{ $json.body.message.toolCalls[0].function.arguments.Hora }}\n”,
“type”: “string”
},
{
“id”: “7007ed32-3afb-4223-82d5-32211a2473c8”,
“name”: “Fecha”,
“value”: “={{ $json.body.message.toolCalls[0].function.arguments.Fecha }}\n”,
“type”: “string”
},
{
“id”: “e169276e-3656-4f3f-ab03-d180088b2b59”,
“name”: “Nombre”,
“value”: “={{ $json.body.message.toolCalls[0].function.arguments.Nombre }}\n”,
“type”: “string”
},
{
“id”: “96e7d5ee-bcb1-47b8-8468-98c1d138c0a3”,
“name”: “Tipo_mesa”,
“value”: “={{ $json.body.message.toolCalls[0].function.arguments.Tipo_mesa }}\n”,
“type”: “string”
},
{
“id”: “e200f7d3-db1f-40f5-9326-1ff5949de001”,
“name”: “Hora_nueva”,
“value”: “={{ $(‘Webhook’).item.json.body.message.toolCalls[0].function.arguments[‘Hora nueva’] }}”,
“type”: “string”
},
{
“id”: “1de3f97e-ce69-4721-a24b-e69fe197ef24”,
“name”: “Fecha_nueva”,
“value”: “={{ $(‘Webhook’).item.json.body.message.toolCalls[0].function.arguments.Fecha_nueva }}”,
“type”: “string”
},
{
“id”: “db07f362-316a-4ed9-a81f-fb96b80a8b65”,
“name”: “Notas_nuevas”,
“value”: “={{ $(‘Webhook’).item.json.body.message.toolCalls[0].function.arguments.Notas_nuevas }}”,
“type”: “string”
},
{
“id”: “7cf1d53b-4124-4cdd-bd22-391e7302c199”,
“name”: “Numero_personas_nuevo”,
“value”: “={{ $(‘Webhook’).item.json.body.message.toolCalls[0].function.arguments.Numero_personas_nuevo }}”,
“type”: “string”
}
]
},
“options”: {}
},
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [
-672,
-208
],
“id”: “ceb81a5a-eed5-4156-bcd2-39c324a6c6b3”,
“name”: “Edit Fields”,
“alwaysOutputData”: true
}
],
“connections”: {
“Edit Fields”: {
“main”: [

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

// 1. Datos del cliente desde el nodo “Edit Fields”
const cliente = $items(“Edit Fields”)[0].json;
const horaCliente = cliente.Hora?.trim();
const fechaCliente = cliente.Fecha?.trim();
const nombreCliente = cliente.Nombre?.trim().toLowerCase();

// 2. Todos los registros del nodo “Search Records”
const reservas = $items(“Search records”).map(item => item.json);

// 3. Buscar coincidencia flexible
// - Coincide siempre por Nombre + Fecha
// - Si el cliente dio una hora, también la comprobamos
const match = reservas.find(r =>
r.Fecha?.trim() === fechaCliente &&
(r.Nombre || “”).trim().toLowerCase() === nombreCliente &&
(!horaCliente || r.Hora?.trim() === horaCliente)
);

// 4. Devolver resultado
if (match) {
return [{
json: {
encontrado: true,
mensaje: Reserva encontrada para ${match.Nombre} el ${match.Fecha} a las ${match.Hora}.,
fields: match
}
}];
} else {
// Opcional: mostrar sugerencias si coincide Nombre + Fecha aunque no coincida la hora
const sugerencias = reservas.filter(r =>
r.Fecha?.trim() === fechaCliente &&
(r.Nombre || “”).trim().toLowerCase() === nombreCliente
);

if (sugerencias.length > 0) {
return [{
json: {
encontrado: false,
mensaje: No se encontró reserva con esa hora, pero sí para ${cliente.Nombre} el ${fechaCliente}. Horas disponibles: ${sugerencias.map(s => s.Hora).join(", ")},
sugerencias,
cliente
}
}];
}

return [{
json: {
encontrado: false,
mensaje: “No existe ninguna reserva con esos datos”,
cliente
}
}];
}

Two things I begged for - to pin the data and to use formatting tags.

Requests fulfilled: 0/2

// 1. Datos del cliente desde el nodo "Edit Fields"
const cliente = $items("Edit Fields")[0].json;
const horaCliente = cliente.Hora?.trim();
const fechaCliente = cliente.Fecha?.trim();
const nombreCliente = cliente.Nombre?.trim().toLowerCase();

// 2. Todos los registros del nodo "Search Records"
const reservas = $items("Search records").map(item => item.json);

// 3. Buscar coincidencia flexible
//    - Coincide siempre por Nombre + Fecha
//    - Si el cliente dio una hora, también la comprobamos
const match = reservas.find(r =>
  r.Fecha?.trim() === fechaCliente &&
  (r.Nombre || "").trim().toLowerCase() === nombreCliente &&
  (!horaCliente || r.Hora?.trim() === horaCliente)
);

// 4. Devolver resultado
if (match) {
  return [{
    json: {
      encontrado: true,
      mensaje: `Reserva encontrada para ${match.Nombre} el ${match.Fecha} a las ${match.Hora}.`,
      fields: match
    }
  }];
} else {
  // Opcional: mostrar sugerencias si coincide Nombre + Fecha aunque no coincida la hora
  const sugerencias = reservas.filter(r =>
    r.Fecha?.trim() === fechaCliente &&
    (r.Nombre || "").trim().toLowerCase() === nombreCliente
  );

  if (sugerencias.length > 0) {
    return [{
      json: {
        encontrado: false,
        mensaje: `No se encontró reserva con esa hora, pero sí para ${cliente.Nombre} el ${fechaCliente}. Horas disponibles: ${sugerencias.map(s => s.Hora).join(", ")}`,
        sugerencias,
        cliente
      }
    }];
  }

  return [{
    json: {
      encontrado: false,
      mensaje: "No existe ninguna reserva con esos datos",
      cliente
    }
  }];
}

Sorry, now?

And the output of the Edit Fields node?

[
  {
    "Hora": "20:00\n",
    "Fecha": "2025-09-25\n",
    "Nombre": "Eric\n",
    "Tipo_mesa": "interior\n",
    "Hora_nueva": "20:00",
    "Fecha_nueva": "2025-09-27",
    "Notas_nuevas": "Alergia al gluten",
    "Numero_personas_nuevo": "2"
  }
]

I did it? ,

Yes you did it!

Ok, so you getting false, because in the Set Node (Edit node) your Hora is “20:00” and in the input to the code node your item which has Eric has Hora as “19:30”.

As soon as you make them match, the result is true.

So the issue was that the .find() function couldn’t find the match.

===

POC:

1 Like

What I’m trying to do with modifying the order is to also be able to change the time and how to change the order to another time. If possible, the time requested and the new time are there, that’s why that happens

Ok, so your question is answered?

If I can get the entire reservation to change to the new time

Sorry I didn’t understand the last message, but if you want to find the match even if the time doesn’t match, then you shouldn’t be including time in the matching pattern.

1 Like

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