0

I'm using express server with ejs templates for client and im passing an object not a single variable from server to client side.. like

router.get('/users/info',(req,res)=>{
  let data = {
    name:"myName",
    email:"me@email.com",
    address:"x,y,z",
    coordinates:{ lat:34.545, lng:78.345 }
 }
res.render('myEJSfile',{data});
})

In myEJSfile.ejs file

<div id="_data" data-params="<%=data.name%>"></div>
<script>
  let data = document.getElementById('_data').dataset.params;
  console.log(data);
</script>

OUTPUT : myName

which is expected output

But when I try to log whole object.. Again in myEJSfile.ejs :

<div id="_data" data-params="<%=data%>"></div>
<script>
  let data = document.getElementById('_data').dataset.params;
  console.log(data);
</script>

OUTPUT : [object Object]

here i want to display a complete valid object passed as a parameter from server side as i need to access on each and every attribute of this object seperately

Guys im stuck i have no other way in this particular situation get me out of this

  • Try `console.log(JSON.stringify(data));` – dikuw Jul 25 '20 at 11:36
  • https://stackoverflow.com/questions/11289793/accessing-ejs-variable-in-javascript-logic – prax Jul 25 '20 at 12:21
  • @dikuw it worked but there is one more thing.. if i want to use name only in script file how may i do that.. if i use `data.name` in **script tag** it displays undefined in console – Muhammad Usman Jul 25 '20 at 12:56

0 Answers0