cannot access the row values of an object using javascript and ejs

117 Views Asked by At

I have an ejs file which looks like below

<!DOCTYPE html>
<html> 
<head> 
 <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
 <title>Google Maps Sensor  Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
      type="text/javascript"></script>
</head> 
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
      <p id="demo"></p>
    <p id="pil"></p>
    <p id="rowc"></p>
 <script>
      function addZero(i) 
      { if (i < 10)
          { i = "0" + i;
            }
          return i;
      }
  var d = new Date(1382086394000);
  //var x = document.getElementById("demo");
  var h = addZero(d.getHours());
  var m = addZero(d.getMinutes());
  var s = addZero(d.getSeconds());
  document.getElementById("demo").innerHTML= h + ":" + m + ":" + s;       
  var myVar = <%- JSON.stringify(jsresult) %>; 
  var count= <%- rowcount %>       

document.getElementById("pil").innerHTML=  myVar.rows[0].sensor;
document.getElementById("rowc").innerHTML=  count;
 </script>
</body>
</html>

which gives me the output like below:

01:53:14

6

7

I am trying to get the values of sensors from myVar by performing an iteration instead of accessing by using

   myVar.row[0].sensor , myVar.rows[1].sensor and so on...

So I added something like this in between the above code

 .
 .
 .

 var myVar = <%- JSON.stringify(jsresult) %>; 
  var count= <%- rowcount %>

  var arlene1= [];   
   for (var j=0; j<=count;j++)
   { arlene1[j] = myVar.rows[j].sensor ; 
   }  

 document.getElementById("pil").innerHTML=  myVar.rows[0].sensor;
document.getElementById("rowc").innerHTML=  count;

.
.
.

it now just throws a blank page without no output.

Can someone explain what is wrong over here??

I am basically trying to get the values of all 7 sensors into an array and then present them on the UI

The debugger console shows the error like below: enter image description here

1

There are 1 best solutions below

8
On

In the last block of code, you've assigned rowcount to the count variable, but in the for loop you're trying to access rowcount. rowcount is undefined when the JavaScript code is evaluated on the client. Try changing it to this:

var myVar = <%- JSON.stringify(jsresult) %>; 
var count= <%- rowcount %>; // 7

var arlene1= [];

// Iterate from 0 to 6
for (var j = 0; j < count; j++)
{
    arlene1[j] = myVar.rows[j].sensor; 
}  

document.getElementById("pil").innerHTML=  myVar.rows[0].sensor;
document.getElementById("rowc").innerHTML=  count;