Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
103
rated 0 times [  105] [ 2]  / answers: 1 / hits: 10072  / 9 Years ago, tue, december 8, 2015, 12:00:00

I'm having a little bit problems, I'm trying to send an object to use on my Javascript client side, whenever I render the view. I am sending it this way (Im using Handlebars)



  > locations=[ {
> local:my_local,
> desc:myDesc
> }];

res.render('myView', { locaciones:locaciones });


// Then in my view Inside a script tag i try to get that var and print it



<script>
var myObj='{{locations}}';
console.log(locations);
</script>


the result is this :



[object]


and I can't use any property of it because it's undefined


More From » node.js

 Answers
3

You can send your object as a string.



Like locations = JSON.stringify({local:my_local,desc:myDesc});



And on client side you can parse it to get an Object



Like loc = JSON.parse(locations);



Now you can use loc as Object.


[#32360] Monday, December 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaley

Total Points: 141
Total Questions: 109
Total Answers: 109

Location: Burundi
Member since Sat, Aug 21, 2021
3 Years ago
;