Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 我的路线怎么了_Meteor_Iron Router - Fatal编程技术网

Meteor 我的路线怎么了

Meteor 我的路线怎么了,meteor,iron-router,Meteor,Iron Router,我正试图将路由更改为,但将router.js中的参数从“_id”更改为“title”时,我得到的是空页面 我有一个表格可以很好地工作: Template.addpost.events({ 'submit form': function(e) { e.preventDefault(); var query = { title: $(e.target).find('[name=title]').val(), te

我正试图将路由更改为,但将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');
    }
  });
和my 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>

{{title}}
{{{text}}}

您需要指定查询条件

你有这个

return Posts.findOne(this.params.friendlyTitle);
换成这个

return Posts.findOne({title:this.params.friendlyTitle});
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}); 
  }
});
如果没有,则查找将采用
this.params.friendlyTitle
作为
\u id
,并返回空查询

路线更干净*

把路线改成这个

return Posts.findOne({title:this.params.friendlyTitle});
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érentes
Router.map
Router.route

要清楚,您需要为帖子/标题和帖子/:id创建单独的路线,对吗?我想bartezr不再需要
帖子/:\u id
路线了