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:"[email protected]",
    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

3
  • Try console.log(JSON.stringify(data)); Commented Jul 25, 2020 at 11:36
  • stackoverflow.com/questions/11289793/… Commented Jul 25, 2020 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 Commented Jul 25, 2020 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.