2015-04-06 58 views
0

我试图将我的路由http://localhost:3000/posts/id更改为http://localhost:3000/posts/title,但是在将router.js中的params从'_id'更改为'title'时,我得到空白页面。我的路由有什么问题

我有一种形式,做工精细:

Template.addpost.events({ 
    'submit form': function(e) { 
     e.preventDefault(); 
     var query = { 
      title: $(e.target).find('[name=title]').val(), 
      text: $(e.target).find('[name=text]').val() , 
      image: $(e.target).find('[name=image]').val(), 
      intro: $(e.target).find('[name=intro]').val(), 
      friendlyTitle: slugify($(e.target).find('[name=title]').val()), 
      author: Meteor.user().profile.name 
     }; 
     query._id = Posts.insert(query); 
     Router.go('index'); 
    } 
    }); 

而且我router.js:

Router.configure({ 
    layoutTemplate: 'layout', 
    loadingTemplate: 'loading', 
    waitOn: function() { return Meteor.subscribe('posts'); } 

}); 

    Router.map(function() { 
     this.route('index', {path: '/'}); 
     this.route('addpost', {path: '/add'}); 
     this.route('postPage', { 
      path: '/posts/:friendlyTitle', //empty page with friendlyTitle, but with _id working good 
      data: function() { console.log(this.params.friendlyTitle);return Posts.findOne(this.params.friendlyTitle); //same, with _id working good} 

     }); 
    }); 
    Router.onBeforeAction('loading'); 

postPage.html:

<template name="postPage"> 
    <div class="container main"> 
     <div class="col-md-12"><h1>{{title}}</h1></div> 
     <div class="col-md-12"> 
      {{{text}}} 
     </div> 
    </div> 
</template> 

回答

2

你需要指定查询条件。

你有这个。

return Posts.findOne(this.params.friendlyTitle); 

更改为此。

return Posts.findOne({title:this.params.friendlyTitle}); 

如果没有发现将采取this.params.friendlyTitle_id将返回空查询

路线更干净*

改变这一路线。

Router.route('/posts/title', { 
    name: 'postPage', 
    waitOn:function(){ 
    return Meteor.subscribe('posts'); //or use the new subscritionReady 
    }, 
    data: function() { 
    console.log(this.params.title) 
    console.log(Posts.findOne({title:this.params.title});) 
    return Posts.findOne({title:this.params.title}); 
    } 
}); 

使用Router.map

Router.map(function() { 
    this.route('postPage', { 
    path: '/posts/:title', 
    waitOn:function(){ 
    return Meteor.subscribe('posts'); //or use the new subscritionReady 
    }, 
    data: function(){ 
     return Posts.findOne({title:this.params.title}); 
    } 
    }); 
}); 

使用différentesRouter.mapRouter.route为每个路由。

+0

要清楚,您需要为posts/title和posts /:id,correect创建单独的路线? –

+2

我不认为bartezr希望'posts /:_ id'路线了 – Ethaan