I'm using express to do a very simple page. My index.js looks like this:
exports.index = function(req, res){
res.render(
'index',
{
title: 'Expressssss',
Tin: varTin,
Tout : varTout,
Hin : varHin,
Tout_array : { 'date': 'Thu Mar 07 2013 22:00:04 GMT+0100 (CET)', 'value': '10062' }
}
);
};
On the file index.ejs I can use the values using for example <%= Tin %>... This works very well....
The problem is that data = <%= Tout_array %>; doesn't seem to work well. Using chromes console, it get an error on this line. The html code after parsing looks like this:
data = [object Object];
and the error at this point is
Uncaught SyntaxError: Unexpected identifier
what am I doing wrong? why parsing any other variale works pretty good, but not this nested json structure? I'm using this, because I want to send a bigger array.
Thans
If you're trying to generate client-side Javascript from an EJS template, I think you want this:
data = <%- JSON.stringify(Tout_array) %>;
Try this:
<%= JSON.stringify(Tout_array) %>