Component.find behavior that you know but may not comply with

Published in Not-So-Secret Diary

Probably, you know that the find() method may return a single object or a list object. But I realized that many developers do not carry about cases when the find() method may return only one object and it causes the ".forEach is not a function" error. It happens because the find() method found only one match, and in this case, return a single object. That was why the forEach() method is not a function for an object, it's only available for lists. However, there exists a simple workaround how to fix that :)

let myResult = component.find("myParam");
myResult = [].concat(myResult);
// myResult is a list now, and you can use the forEach() method

Useful links:

Comments powered by CComment