Gaurav Gupta
Gaurav Gupta

Reputation: 4701

undefined is not a function in ejs

As i am trying to dive into js world, its my first attempt to encounter and learn ejs.

Now my problem is i am getting following error:

for(i in data.result){ 22| var intime = data.result[i].intime; undefined is not a function

and related code goes like following:

     <table >
          <thead>
            <th>Project Name</th><th>Date</th><th>Login</th><th>Logout</th><th>Time Spend</th>
          </thead> 
          <%
            var fraction = 1000 * 60 * 60;
            for(i in data.result){
                var intime = data.result[i].intime;
                var outtime = data.result[i].outtime;
                var difftime = ((outtime.getTime() - intime.getTime()) / fraction);
                var date = data.result[i].date;
          %>
                <tr>
                  <td>Ninja</td>
                  <td><%=date%></td>
                  <td><%=intime%></td>
                  <td><%=outtime%></td>
                  <td><%=difftime%></td>
                </tr>
          <%
            }
          %>
</table>

My Attempt:

As exception is saying that something is undefined and i am trying to call the function on it, so exception is raised. So to verify this i omiited the whole table tag and added following code only;

 <%= JSON.stringify(data.result[0].outtime)%>   
     <br>
     <%= JSON.stringify(data.result[1])%>   
     <br>
     <%= JSON.stringify(data.result)%>

which resulted in

"14:44:45" 
{"empId":3,"projectId":1,"intime":"09:44:45","outtime":"14:44:45","date":"2014-08-05T18:30:00.000Z"} 
[{"empId":3,"projectId":1,"intime":"09:44:45","outtime":"14:44:45","date":"2014-08-04T18:30:00.000Z"},{"empId":3,"projectId":1,"intime":"09:44:45","outtime":"14:44:45","date":"2014-08-05T18:30:00.000Z"}]  

Now i am not able to figure out what is the problem and what mistake i am making here.

Thanks

Upvotes: 1

Views: 570

Answers (1)

mscdex
mscdex

Reputation: 106746

You're trying to use a string (data.result[0].outtime and presumably data.result[0].intime as well) as if it were a Date object: ((outtime.getTime() - intime.getTime()) / fraction)

getTime() is not a function available for strings (thus undefined), so that is why you're seeing that particular error.

Upvotes: 1

Related Questions