Element = null instead of a number

Hello, I am currently stuck on my code, I have a problem reading a data. Here is the code

for (const item of $input.all()) {
const notionData = $items("Notion (Beta)1");

function searchElement(item, notionData, colonne, nom, elemntdecolonne) {
  notionData.forEach(itemR2D2 => {
    const valeurdef = itemR2D2[
"property_revenu_ht"];
    item.json.property_nb_de_luminaire = 0;
    item.json.property_nb_de_luminaire += valeurdef;
  });
}
  
  searchElement(item, notionData, "property_prix_photographe_ttc", "Photo", "property_pvht_photographe_ht");
  searchElement(item, notionData, "property_prix_montage_ttc", "Montage", "property_pvht_montage_coordination");
}

return $input.all();

I managed to solve the problem a bit

let notionData = $items("Notion (Beta)1");
let autredata = $items("Split In Batches");

autredata[0].json.property_temps_total = notionData[0].json.property_total_ht
const global = {
  notionData,
  autredata
}


return global;

in fact I am blocked again, at the same place, I think there is a bug with n8n

Hi @simon_Piquemal

It would help if you tell us what the issue is and what you are expecting as the result.

yes, excuse me for being imprecise. The result should be the value of the other elements. To be clearer here are 2 examples of my case. (I modified the structure of my code but the system remains the same).

the code:

const notionData = $items("Notion (Beta)1");
let autredata = $items("Split In Batches");

notionData.forEach(element => {
  if(element.name == autredata[0].property_type_de_bien){
  let services = autredata[0].json.property_services;
  const prixmontage = element.property_pvht_montage_coordination;
    if (Array.isArray(services)) {
      let montageService = services.find(service => service === "Montage");
      if (montageService) {
        autredata[0].json.property_temps_total = 67 ;
      }
    }
    
  }
 })
return autredata;
  1. I assign 67 to otherdata[0].json.property_time_total.
    As you can see the condition is correct and I get 67 in property_time_total. So the 2 tables are read correctly.

  2. I assign element.property_pvht_montage_coordination to otherdata[0].json.property_time_total instead of 67. The base value of element.property_pvht_montage_coordination is 1390. So I should have as result property_time_total = 1390 but I opt for null instead.

const notionData = $items("Notion (Beta)1");
let autredata = $items("Split In Batches");

notionData.forEach(element => {
  if(element.name == autredata[0].property_type_de_bien){
  let services = autredata[0].json.property_services;
  const prixmontage = element.property_pvht_montage_coordination;
    if (Array.isArray(services)) {
      let montageService = services.find(service => service === "Montage");
      if (montageService) {
        autredata[0].json.property_temps_total = prixmontage ;
      }
    }
    
  }
 })
return autredata;


as you could see I don’t even have the value returned

Well I wanted to update n8n because maybe it was a bug and that an update would fix it but now I have “Bad Gateway”, the problems follow each other …

Well I reinstalled everything, but I still have the same problem with the code.

I did a lot of testing and something less complex like that:

let notionData = $("Notion (Beta)1").all();
let autredata = $("Split In Batches").all();

autredata[0].json.property_temps_total = 89;
autredata[0].json.property_temps_total = 99;


if (autredata[0].json.property_services.find(element => element === "Montage")){
  autredata[0].json.property_temps_total = notionData[0].json.property_revenu_ht;
}
return autredata;

works well but the problem must be with the .forEach method

I found a workaround with another approach. I think it would be interesting to add more suggestions directly from the editor. :smile:

let notionData = $("Notion (Beta)1").all();
let autredata = $("Split In Batches").all();

function findMatchingIndex(notionData, autredata) {
  for (let i = 0; i < notionData.length; i++) {
    if (notionData[i].json.name === autredata[0].json.property_type_de_bien) {
      return i;
    }
  }
  return -1; // Retourne -1 si aucun élément correspondant n'est trouvé
}

// Utilisation de la fonction pour trouver l'index correspondant
const matchingIndex = findMatchingIndex(notionData, autredata);


autredata[0].json.property_temps_total = 89;
autredata[0].json.property_temps_total = 99;


if (autredata[0].json.property_services.find(element => element === "Montage")){
  autredata[0].json.property_temps_total = notionData[matchingIndex].json.property_revenu_ht;
}

return autredata;
1 Like

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