I have a pretty large object that I need to pass to a function in a client script. I have tried using JSON.stringify, but have run into a few issues with this approach - mostly performance related. Is it possible to do something like this in ejs?

app.get('/load', function(req, res) { var data = { layout:'interview/load', locals: { interview: '', data: someLargeObj } }; res.render('load', data); }); 

And in my client script, I would pass this object to a function like so

<script type="text/javascript"> load(<%- data %>); // load is a function in a client script </script> 

When I try this I get either

<script type="text/javascript"> load(); </script> 

or

<script type="text/javascript"> load([Object object]); </script> 
4

5 Answers

In Node.js:

res.render('mytemplate', {data: myobject});

In EJS:

<script type='text/javascript'> var rows =<%-JSON.stringify(data)%> </script> 

SECURITY NOTE : Don't use this to render an object with user-supplied data. It would be possible for someone like Little Bobby Tables to include a substring that breaks the JSON string and starts an executable tag or somesuch. For instance, in Node.js this looks pretty innocent...

var data = {"color": client.favorite_color} 

but could result in a client-provided script being executed in user's browsers if they enter a color such as:

"titanium </script><script>alert('pwnd!')</script> oxide" 

If you need to include user-provided content, please see for a better answer using Base64 encoding

11

That is the expected behavior. Your template engine is trying to create a string from your object which leads to [Object object]. If you really want to pass data like that I think you did the correct thing by stringifying the object.

1

If you are using templating, then it would be much better to get the values in the template, for example whether user is signed in or not. You can get the send local data using

<script> window.user = <%- JSON.stringify(user || null) %> </script> 

From the server side code, you are sending user data.

res.render('profile', { user: user.loggedin, title: "Title of page" }); 

Think there's a much better way when passing an object to the ejs , you dont have to deal with JSON.stringfy and JSON.parse methods, those are a little bit tricky and confusing. Instead you can use the for in loop to travel the keys of your objects, for example:

if you have an object like such hierarchy

{ "index": { "url": "/", "path_to_layout": "views/index.ejs", "path_to_data": [ "data/global.json", { "data/meta.json": "default" } ] }, "home": { "url": "/home", "path_to_layout": "views/home/index.ejs", "path_to_data": [ "data/global.json", { "data/meta.json": "home" } ] }, "about": { "url": "/about", "path_to_layout": "views/default.ejs", "path_to_data": [ "data/global.json", { "data/meta.json": "about" } ] } } 

On the EJS side you can loop yourObject like this;

<% if ( locals.yourObject) { %> <% for(key in yourObject) { %> <% if(yourObject.hasOwnProperty(key)) { %> <div> <a href="<%= yourObject[key]['subkey'] %>"><%= key %></a></div> <% } %> <% } %> <% } %> 

For this example [key] can take 'index','home' and 'about' values and subkey can be any of it's children such as 'url','path_to_layout','path_to_data'

What you have is a result like this [{'re': 'tg'}]

You actually need to loop it. See javascript while loop

Then, render it in your front end with ejs... i can't help on that, i use hbs

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy