1
votes

I have this basic route in place and my template does not receive the data I am setting.

App =
    subs:
        posts: Meteor.subscribe "posts"
        users: Meteor.subscribe "users"

Router.configure
    wait: [App.subs.posts, App.subs.users]
    loadingTemplate: 'loading'

Router.map ->
    @route 'post_show',
        path: '/p/:slug'
        before: ->
            if App.subs.post
                App.subs.post.stop()
            App.subs.post = Meteor.subscribe "post", @params.slug

        waitOn: ->
            App.subs.post

        after: ->
            if @getData().post
                document.title = @getData().post?.title

        data: ->
            p = Posts.findOne
                $or: [{
                    slug: @params.slug
                }, {
                    _id: @params.slug
                }]

            date = new Date p.timestamp
            date_added = "{0}-{1}-{2}".format date.getFullYear(), date.getMonth()+1, date.getDate()

            data =
                post: p
                date_added: date_added
            data
        template: 'post_show'

Server code:

Meteor.publish "posts", ->
    return Posts.find()

Meteor.publish "post", (id_or_slug) ->
    return Posts.find({
        $or: [{
            slug: id_or_slug
        }, {
            _id: id_or_slug
        }]
    })

Meteor.publish "userData", ->
    return Meteor.users.find {},
        fields:
            profile: 1

When I refresh the page the p variable inside the data method is undefined. Any ideas why?

1

1 Answers

0
votes

It looks like you have two publications ("posts", and "post") on the same back-end collection ("posts"). As far as I know that's not possible, because the client-side collection needs to match the collection name on the server side. The name under which you publish it doesn't matter so much.

This is also explained in the "counts-by-room" example of http://docs.meteor.com/#meteor_publish.

This raises the question why you would first publish all the posts and then, in addition, publish a subset of that. Can't you just do that subsetting on the client then?