1

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

Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
mo0206
  • 791
  • 5
  • 20
  • 36

1 Answers1

0

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;
soulprovidr
  • 754
  • 3
  • 14
  • i have tried that way too. i forgot to change the code here. I still get a blank page even after giving count or making the loop run for 6 times instead of count – mo0206 Nov 29 '14 at 08:28
  • @MonicaThaneer: Check my edit - there was a syntax error in the `for` loop, too. You're using a comma rather than a semi-colon. – soulprovidr Nov 29 '14 at 08:52
  • I get the time printed now. but still i do not get the 'pil' and 'rowc' values displayed. – mo0206 Nov 29 '14 at 09:02
  • if i remove the loop , the 'pil' and 'rowc' values are displayed. but i want fetch the values of sensor using a loop :-| – mo0206 Nov 29 '14 at 09:02
  • Hi, sorry for not responding - I fell asleep! Try using the Google Chrome JavaScript console to see if you're getting any errors when you load the page. It's really handy in situations like this, when there's a pesky syntax error that isn't immediately obvious. Here is a good intro to using it: https://developer.chrome.com/devtools/docs/console – soulprovidr Nov 29 '14 at 15:38
  • it says cannot read property sensor of undefined. but why is that so .. when i use myVar.rows[0].sensor i dont get any error and when i say myVar.rows[j].sensor it throws an error. – mo0206 Nov 29 '14 at 20:44
  • I have added the console image in the question above – mo0206 Nov 29 '14 at 20:54
  • @MonicaThaneer: Try changing the for loop condition to `j < count`. I think, currently, `j` is incremented all the way up to 7, but `myVar.rows[6]` is the last element in the array. See my edited answer. – soulprovidr Nov 29 '14 at 21:55