我的路由有什么问题

What is wrong with my routing

我正在尝试更改我的路线 http://localhost:3000/posts/id to http://localhost:3000/posts/title,但是当将 router.js 中的参数从“_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>

您需要指定查询条件。

你有这个。

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

改成这个。

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

如果不是,查找将像 _id 一样采用 this.params.friendlyTitle 并将 return 和空查询

路线更干净*

将路线更改为此。

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});
    }
  });
}); 

对每条路线使用不同的 Router.mapRouter.route