Search code examples
javascriptnode-red

Node red Cannot read property 'Symbol(Symbol.iterator)' of undefined


I have this problem on a small programm that I don't really understand (I am kinda new to node red), the code is

var profile = msg.user.profile;
var cart    = profile.cart = profile.cart || [];
var search  = profile.search;
var id      = msg.payload.substring(8);

for (let item of search){
  if ( item.id != id) continue;
  cart.push(item);
  msg.payload = item;
}

And the complete error is TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined on the line for (let item of search){. I did some researches and found out that this error usually is due to something that is undefined, but I don't really understand how to solve this.


Solution

  • I see you are using let syntax; let syntax only available on ecmascript 2015 up (javascript version), so what basically you need to do is to rewrite your code like this

    var profile = msg.user.profile;
    var cart    = profile.cart = profile.cart || [];
    var search  = profile.search;
    var id      = msg.payload.substring(8);
    
    // assuming that profile.search is an array
    for (var i = 0 ; i < search.length ; i++){
      var item = search[i];
      if ( item.id != id) continue;
      cart.push(item);
      msg.payload = item;
    }
    
    // if it is an object, then you could loop through its props
    for (var prop in search ){
       var item = search[prop];
       if ( item.id != id) continue;
       cart.push(item);
       msg.payload = item;
    }