I loop through a list to print to a PDF using jsPDF:
for (i = 0; i <= SRUreport.lista_wps().length; i++) {
valor_imprimir = (i+' : '+Ejemplolista[i])[0].trim());
doc.text(20,100,valor_imprimir);
alert(valor_imprimir);
}
it alert
works correctly, and it throws me the values of Ejemplolista
in pop-up windows.
However, if I use the same variable ( valor_imprimir
) to include it in a PDF using jsPFD it throws me the following error:
Uncaught TypeError: Cannot read property '0' of undefined
Why doesn't it recognize the value [0]
that belongs to the first string of the array Ejemplolista
?
As rnd comments if there is an element in the list that is
undefined
, you will get the error:Although you comment that you can correctly print the values it has
Ejemplolista
, the problem in this case seems to me to be because you don't need the[0]
in this line of code:deals only with (you also have an unnecessary parenthesis):
Another important detail that could cause a bug (indexoutofbounds) is that the number of elements in
SRUreport.lista_wps().length;
can be greater or less than the elements inside:Ejemplolista