Sechiro
Sechiro

Reputation: 103

get an object _id by clicking in meteor.js


Here is my issue: I would like to try dealing with posts on meteor, so I created a schema of posts and a way to create them. Then I wanted to see all the posts created by a user, so I listed them on a view. But now I'd like to reach a particular post through that list. It's quite the same as we can do in the "Your Second Meteor Application" but it doesn't work...


Router.route('/posts', {
    name: "posts",
    data: function(){
        var posts = Posts.find();

        return {
            posts: posts
        };
    },
    waitOn: function(){
        return Meteor.subscribe("userPosts",Meteor.user().username);
    }
});

Router.route('/post/:_id', {
    name: "post",
    data: function(){
        return {
            posts: posts = Posts.findOne({_id:id})
        };
    },
    waitOn: function(){
        return Meteor.subscribe("userPosts",Meteor.user().username);
    }
});


<template name='posts'>
    <h3>Nouveau post</h3>
    <form>
        <label>Titre</label><input type="text" name="titre" /><br />
        <label>Contenu</label><textarea name="contenu" ></textarea><br />
        <button type="submit" >Envoyer</button>
    </form>

    <h3>Posts list</h3>
    <ul>
    {{#each posts}}
        <li><a href="/post/{{_id}}">{{ title }}</a></li>
    {{/each}}
    </ul>

</template>

<template name='post'>
    <h2>Here is your post:</h2>
    <div>
        <p>{{title}} , by {{author}}</p>
        <p>{{content}}</p>
    </div>

</template>

And I get something like:


Here is your post:
, by


hope you guys can help me, thanks.

Edit:

Problem solved thanks to Philip.

Upvotes: 0

Views: 44

Answers (1)

Philip Pryde
Philip Pryde

Reputation: 940

Two things to try, first to use the _id in the url you need to use this.params._id not id in your data and secondly just return the findOne directly as opposed to setting it to data.posts

data: function(){ 
    return Posts.findOne({_id:this.params._id});
}

Upvotes: 1

Related Questions