Object UNDEFINED Error for existing Dom Objects

huangapple go评论69阅读模式
英文:

Object UNDEFINED Error for existing Dom Objects

问题

以下是您要翻译的内容:

"Dom is showing the Nodelist[i].properties of document.querySelectorAll("tr") as 'UNDEFINED' eventhough they exist as OBJECT's and can be accessed and iterated over. Specifically for .innerText ,the node text gets returned as string but still the Dom gives an Undefined error. Why is the Dom not recognising the object type and its properties?

CONSOLE:

querySelectorAll("tr") dtype,string,object = object,[object NodeList],NodeList[]  
rowArray[0] = (string), 	PurchaseTime	Expiry	Product	Temperature	Color  
tables1 copy.html:66 rowArray[1] = (string), 1	12-May-2023 13:29:09	18-May-2023	Apple	40	red
tables1 copy.html:66 rowArray[2] = (string), 2	12-May-2023 13:31:08	18-May-2023	Banana	10	black
tables1 copy.html:64 Uncaught TypeError: Cannot read properties of undefined (reading 'innerText')
    at tablesCssAutoWidth (tables1 copy.html:64:27)

Code(+Table) :

<!DOCTYPE html>
<html lang="en">
<head> 
</head>
<body> 
    <table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>PurchaseTime</th>
      <th>Expiry</th>
      <th>Product</th>
      <th>Temperature</th>
      <th>Color</th>
  
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>12-May-2023 13:29:09</td>
      <td>18-May-2023</td>
      <td>Apple</td>
      <td>40</td>
      <td>red</td>
 
    </tr>
    <tr>
      <th>2</th>
      <td>12-May-2023 13:31:08</td>
      <td>18-May-2023</td>
      <td>Banana</td>
      <td>10</td>
      <td>black</td>
     
    </tr>
  </tbody>
</table>
  
</body>
</html>
<script>
  window.onload = function tablesCssAutoWidth(){


    const tableNodelist_tr = document.querySelectorAll("tr");
    console.log("querySelectorAll(tr) dtype = %s,%s,%o" ,typeof(tableNodelist_tr),tableNodelist_tr,tableNodelist_tr)

    // const tableNodelist_tr = document.querySelectorAll("tr");
var row_allArray = []
for (let i = 0; i <= tableNodelist_tr.length; i++) {

    let row_idata = tableNodelist_tr[i]
    row_idata = row_idata.innerText
    let row_idtype = typeof(row_idata)
    console.log("rowArray[%s] = (%s), %s",i,row_idtype,row_idata)

    row_iArray =  row_idata.replace(/\s/g, ",").split(',');
    row_allArray.concat( row_iArray)}


console.log("querySelectorAll(tr)[1].innerText dtype = %s,%s" ,typeof(tableNodelist_tr.innerText[1]),tableNodelist_tr.innerText[1])

console.log("row_allArray= %s",row_allArray)

for (let i = 1; i <= tableNodelist_tr.length; i++) {  

  if(
  parseInt(row_allArray[i][5]) <20 |
  row_iArray[i][6] == "black" ){
  row_iArray[i].style.backgroundColor = "red";}
  
}
}
</script>
英文:

Dom is showing the Nodelist[i].properties of document.querySelectorAll("tr") as 'UNDEFINED' eventhough they exist as OBJECT's and can be accessed and iterated over.
Specifically for .innerText ,the node text gets returned as string but still the Dom gives an Undefined error.

Why is the Dom not recognising the object type and its properties?

CONSOLE:

querySelectorAll("tr") dtype,string,object = object,[object NodeList],NodeList[]  
rowArray[0] = (string), 	PurchaseTime	Expiry	Product	Temperature	Color  
tables1 copy.html:66 rowArray[1] = (string), 1	12-May-2023 13:29:09	18-May-2023	Apple	40	red
tables1 copy.html:66 rowArray[2] = (string), 2	12-May-2023 13:31:08	18-May-2023	Banana	10	black
tables1 copy.html:64 Uncaught TypeError: Cannot read properties of undefined (reading 'innerText')
    at tablesCssAutoWidth (tables1 copy.html:64:27)

Code(+Table) :

<!DOCTYPE html>
<html lang="en">
<head> 
</head>
<body> 
    <table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>PurchaseTime</th>
      <th>Expiry</th>
      <th>Product</th>
      <th>Temperature</th>
      <th>Color</th>
  
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>12-May-2023 13:29:09</td>
      <td>18-May-2023</td>
      <td>Apple</td>
      <td>40</td>
      <td>red</td>
 
    </tr>
    <tr>
      <th>2</th>
      <td>12-May-2023 13:31:08</td>
      <td>18-May-2023</td>
      <td>Banana</td>
      <td>10</td>
      <td>black</td>
     
    </tr>
  </tbody>
</table>
  
</body>
</html>
<script>
  window.onload = function tablesCssAutoWidth(){


    const tableNodelist_tr = document.querySelectorAll("tr");
    console.log("querySelectorAll(tr) dtype = %s,%s,%o" ,typeof(tableNodelist_tr),tableNodelist_tr,tableNodelist_tr)

    // const tableNodelist_tr = document.querySelectorAll("tr");
var row_allArray = []
for (let i = 0; i <= tableNodelist_tr.length; i++) {

    let row_idata = tableNodelist_tr[i]
    row_idata = row_idata.innerText
    let row_idtype = typeof(row_idata)
    console.log("rowArray[%s] = (%s), %s",i,row_idtype,row_idata)

    row_iArray =  row_idata.replace(/\s/g, ",").split(',');
    row_allArray.concat( row_iArray)}


console.log("querySelectorAll(tr)[1].innerText dtype = %s,%s" ,typeof(tableNodelist_tr.innerText[1]),tableNodelist_tr.innerText[1])

console.log("row_allArray= %s",row_allArray)

for (let i = 1; i <= tableNodelist_tr.length; i++) {  

  if(
  parseInt(row_allArray[i][5]) <20 |
  row_iArray[i][6] == "black" ){
  row_iArray[i].style.backgroundColor = "red";}
  
}
}
</script>

答案1

得分: 0

Cannot read properties of undefined错误来自于for循环中的条件错误:

for (let i = 0; i < tableNodelist_tr.length; i++) {

在这段代码中,迭代器将遍历0123,但是tableNodelist_tr只有索引到2的位置。这意味着以下语句:

let row_idata = tableNodelist_tr[i] // = tableNodelist_tr[3]

会被评估为undefined。因此,在此之后的语句:

row_idata = row_idata.innerText // = undefined.innerText

试图读取undefinedinnerText属性,这是无效的。

解决方法是将条件从小于或等于 (<=) 调整为 小于 (<):

for (let i = 0; i < tableNodelist_tr.length; i++) {
英文:

The Cannot read properties of undefined error comes from seems to be an incorrect condition in the for loop:

for (let i = 0; i &lt;= tableNodelist_tr.length; i++) {

With this code, the iterator will go through 0, 1, 2 and 3, but the tableNodelist_tr only has indices up to 2. This means that the following statement:

let row_idata = tableNodelist_tr[i] // = tableNodelist_tr[3]

Evaluates to undefined. Thus, the following statement after:

row_idata = row_idata.innerText // = undefined.innerText

Tries to read the innerText property of undefined which is invalid.

The solution is to adjust the condition from less than or equal to (&lt;=) to less than (&lt;):

for (let i = 0; i &lt; tableNodelist_tr.length; i++) {

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.onload = function tablesCssAutoWidth() {
  const tableNodelist_tr = document.querySelectorAll(&quot;tr&quot;);
  console.log(&quot;querySelectorAll(tr) dtype = %s,%s,%o&quot;, typeof(tableNodelist_tr), tableNodelist_tr, tableNodelist_tr)

  // const tableNodelist_tr = document.querySelectorAll(&quot;tr&quot;);
  var row_allArray = []
  for (let i = 0; i &lt; tableNodelist_tr.length; i++) {

    let row_idata = tableNodelist_tr[i]
    row_idata = row_idata.innerText
    let row_idtype = typeof(row_idata)
    console.log(&quot;rowArray[%s] = (%s), %s&quot;, i, row_idtype, row_idata)

    row_iArray = row_idata.replace(/\s/g, &quot;,&quot;).split(&#39;,&#39;);
    row_allArray.concat(row_iArray)
  }

  console.log(&quot;querySelectorAll(tr)[1].innerText dtype = %s,%s&quot;, typeof(tableNodelist_tr.innerText[1]), tableNodelist_tr.innerText[1])

  console.log(&quot;row_allArray= %s&quot;, row_allArray)

  for (let i = 1; i &lt;= tableNodelist_tr.length; i++) {

    if (
      parseInt(row_allArray[i][5]) &lt; 20 |
      row_iArray[i][6] == &quot;black&quot;) {
      row_iArray[i].style.backgroundColor = &quot;red&quot;;
    }

  }
}

<!-- language: lang-html -->

&lt;table border=&quot;1&quot; class=&quot;dataframe&quot;&gt;
  &lt;thead&gt;
    &lt;tr style=&quot;text-align: right;&quot;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;PurchaseTime&lt;/th&gt;
      &lt;th&gt;Expiry&lt;/th&gt;
      &lt;th&gt;Product&lt;/th&gt;
      &lt;th&gt;Temperature&lt;/th&gt;
      &lt;th&gt;Color&lt;/th&gt;

    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;12-May-2023 13:29:09&lt;/td&gt;
      &lt;td&gt;18-May-2023&lt;/td&gt;
      &lt;td&gt;Apple&lt;/td&gt;
      &lt;td&gt;40&lt;/td&gt;
      &lt;td&gt;red&lt;/td&gt;

    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;12-May-2023 13:31:08&lt;/td&gt;
      &lt;td&gt;18-May-2023&lt;/td&gt;
      &lt;td&gt;Banana&lt;/td&gt;
      &lt;td&gt;10&lt;/td&gt;
      &lt;td&gt;black&lt;/td&gt;

    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 10:20:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定