How is the JSON.parse data in a Meteor template?

Here is my scenario, the first project with Meteor : I have JSON string data embedded in my MongoDB documents, as you can see in the MESSAGE section below:

{ "DATE" : "Jan 24 23:28:14", "MESSAGE" : **"{\"status_code\":200,\"uri\":\"172.16.156.143/content/dynamic\",\"tprocess\":3}"**, "_id" : ObjectId("5101c3992a1624e726000014") } 

In Meteor, the code for my client template is as follows:

 Template.log.logEntry = function () { return Messages.find({}); }; 

... Which works fine, but will obviously display the following pattern ...

 <template name="log"> <h1>Log Entries Are:</h1> {{#each logEntry}} <div> : {{MESSAGE}} </div> {{/each}} </template> 

... if the browser has an uninfected literal string,

 {"status_code":200,"uri":"172.16.156.143/static/style.css","tprocess":2} 

I like JSON.parse () this line and do more interesting things, but I'm not sure how best to do this from the isClient context in my Meteor project JavaScript file.

+4
source share
2 answers

Add a template helper:

 Template.log.aSpecificField = function () { return JSON.parse(this.MESSAGE).aSpecificField; } 

which allows you to use {{aSpecificField}} in your #each loop.

+7
source

I'm not sure if this is the best way to do this (indeed, I suspect not), but it works as expected.

 Template.log.helpers({ get_uri: function () { return JSON.parse(this.MESSAGE).uri; } 

Calling it in the template is now simple:

 {{ get_uri }} 
+2
source

All Articles