Confusing function node problem

Hi,

I have a function node with some if statements to handle some conditional re-mapping of items.

This foreach loop was working without my if portion, but now I’m getting an error “newitem is not defined”, without saying which line the problem is.

I’m still learning javascript, so I’ve reached the end of what I can think of on this:

Node Code:

const newItems = [];
let newItem = [];

let mapping = $item(0).$node["Settings"].json["query"]["mapping"];
let item;

if (mapping = "test") 
  {
  
  items.forEach(item => {
      item = item.json;
  
      //newItem = {};
  
      newItem['ENTTLA2'] = item['ENTTLA'];
      newitem['CableDescription1'] = item['CableDescription1'];
      newitem['CableDescription2'] = item['CableDescription2'];
      newitem['CableDescription3'] = item['CableDescription3'];
      newitem['EndEquipment'] = item['EndEquipment'];
      newitem['EndEquipmentPosition'] = item['EndEquipmentPosition'];
      newitem['EndRoom'] = item['EndRoom'];
      newitem['KTR'] = item['KTR'];
      newitem['Kabelnummer'] = item['Kabelnummer'];
      newitem['Description'] = item['Description'];
      newitem['SRTP'] = item['SRTP'];
      newitem['StartEquipment'] = item['StartEquipment'];
      newitem['StartEquipmentPosition'] = item['StartEquipmentPosition'];
      newitem['StartRoom'] = item['StartRoom'];
      newitem['Status'] = item['Status'];
      newitem['CableType'] = item['CableType'];
      newitem['BGR'] = item['BGR'];
      //newItem['canonical-vid'] = item['canonical-vid'];
  
      // TODO: Keep on going from here by adding everything as you want it
    
      newItems.push({ json: newItem });
      });
  };

if (mapping = "none") 
  {
  

  items.forEach(item => {
      item = item.json;
  
      newItem = {};
      newItem['ENTTLA3'] = item['ENTTLA'];
      
      newItems.push({ json: newItem });
      });
  };

return newItems;

Error Message:

ERROR: newitem is not defined

Details

Stack

ReferenceError: newitem is not defined
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:16:7
    at Object.base.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/contextify.js:228:34)
    at Array.forEach (<anonymous>)
    at Object.base.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/contextify.js:621:32)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:10:9
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:54:18)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1167:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:81:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:508:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:442:62

Thanks for any insight!

Hey @wsargent,

You want to use the == instead of = in the IF node for the condition. Also, in your code, you’re using newitem instead of newItem. Since newitem is not defined, you’re getting this error.

1 Like

Thanks so much Harshil! I should have caught the newitem case problem. Adding the == to my javascript knowledge (about the 3rd piece of info so far… lol)

Pushing hard to learn all I can, just also have to keep my day job moving forward!

No worries!

There’s an alternate approach that involves almost no code. You can use the Switch node (or IF node) to check for the condition, and then a Set node to set these values.

Yeah, that’s a great option as well for smaller sets. I looked at that, but in this particular case, I could have up to 100 mapping values across multiple IF’s, so I wanted to put it in a function node so I can copy paste my JS mapping logic from a spreadsheet that tracks the mappings, and calculates the JS for me.

The fully no-code would be a bit tedious… :slight_smile:

Thanks again for the suggestion!

1 Like

In this case, it makes sense! Wondering if duplicating the nodes would have been helpful