I know there are a lot of same questions already posted in stack-overflow and tried different solutions to avoid the run-time error but None of them are working for me.
setTimeout(() => { // your code here }, 0);
I wrapped my code in setTimeout and it worked