Greetings to all! Thank you for your assistance. The question has been revised based on the feedback received.
I'm a newbie when it comes to Mongo and Meteor.
In my "posts" collection, there is a field called "slug".
The "post" template is correctly populating with values of each post. The slug value always follows a format like "my-great-post".
My task is to retrieve the text value for the _id's slug, which will vary each time the template is accessed. I need to encode it, generate a string, and then display the string in the template.
Attempts Made:
Failed to return a value for "this.slug" or "this.data.slug" in either template helpers or onRendered, despite having the collection defined and accurately populating spacebars values in the template
When using "this", it outputs "[object Object]" to the console.log
The app crashes when trying to encode and deliver a string from the helper function. Perhaps, I do not fully grasp the syntax for helpers as per the documentation
(Per advice in the comments, I refrained from attempting to create scripts in the template HTML. Here is additional information requested by those providing assistance)
- Template HTML -
{{#with post}}
<div class="blog-article">
<div class="blog-header">
<div class="left">
<!-- title -->
<h1 class="post-title">{{title}}</h1>
<div class="holder">
<div class="post-tags">
<!-- tags -->
{{#each tags}}
<span>{{this}}</span>
{{/each}}
</div>
</div>
</div>
</div>
<div class="blog-post">
<div class="blog-copy">
<!-- date -->
<div class="post-date">{{post_date}}</div>
<!-- social -->
<div class="blog-social">
<!--
<a class="so-facebook" target="_blank" href="need to encode slug here"></a>
-->
</div>
<!-- ============== post ============== -->
{{{content}}}
<!-- ============ end post ============ -->
</div>
</div>
</div>
{{/with}}
- Template JS -
Template.post.onCreated(function() {
var self = this;
self.autorun(function() {
var postSlug = FlowRouter.getParam('postSlug');
self.subscribe('singlePost', postSlug);
});
});
Template.post.helpers({
post: function() {
var postSlug = FlowRouter.getParam('postSlug');
var post = Posts.findOne({slug: postSlug}) || {};
return post;
}
// Unable to make these work within a helper function; they crash the app outside the helper
// console.log(this.slug);
// console.log(this.data.slug);
});
Template.post.onRendered( function () {
// These are ineffective
// console.log(this.slug);
// console.log(this.data.slug);
});
db.posts.findOne();
{
"_id" : ObjectId("576c95708056bea3bc25a91f"),
"title" : "How Meteor Raised the Bar For New Rapid-Development Technologies",
"post_date" : "May 28, 2016",
"image" : "meteor-raised-the-bar.png",
"slug" : "how-meteor-raised-the-bar",
"bitlink" : "ufw-29Z9h7s",
"tags" : [
"Tools",
"Technologies"
],
"excerpt" : "sizzling excerpt",
"content" : "bunch of post content html"
}
If someone can resolve this using any method, I would be overjoyed and immensely grateful for the solution.