Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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
Node.js 如何在使用iron:meteor上的路由器进行路由之前通过会话?_Node.js_Meteor_Session Variables_Iron Router - Fatal编程技术网

Node.js 如何在使用iron:meteor上的路由器进行路由之前通过会话?

Node.js 如何在使用iron:meteor上的路由器进行路由之前通过会话?,node.js,meteor,session-variables,iron-router,Node.js,Meteor,Session Variables,Iron Router,在我的post_item.html中,我得到了这一行: <a href="{{pathFor 'postPage' _id=this._id}}" class="discuss btn">Discuss</a> 在我的post_page.js中 Template.postPage.helpers({ currentPost: function() { return Posts.findOne(Session.get('currentPostId')

在我的post_item.html中,我得到了这一行:

 <a href="{{pathFor 'postPage' _id=this._id}}" class="discuss btn">Discuss</a>
在我的post_page.js中

Template.postPage.helpers({
     currentPost: function() {
     return Posts.findOne(Session.get('currentPostId'));
     }
});
在我的post_page.html中

<template name="postPage">
 {{#with currentPost}}
 {{> postItem}}
 {{/with}}
</template>

{{#带currentPost}
{{>positem}
{{/与}}
我做错了什么?
我如何将ID传递到新的“页面”并显示结果?

不确定为什么需要会话,但您的路线看起来是错误的。请尝试以下方法:

Router.map(function() {
    this.route('postPage',{path:'/posts/:_id',
    onBeforeAction: function() {
        console.log("hello!");
       Session.set('currentPostId', this.params._id); 
    }
  });
});

我假设您正在使用《发现流星》一书编写流星教程。我面临着同样的问题,但是,我能够在不使用会话的情况下解决它。如果你使用,你不再需要任何助手

client/router.js:

  Router.map(function() {
    this.route('postPage',{path:'/posts/:_id',
    beforeRoutingTo: function(id) {
        console.log("hello!");
       Session.set('currentPostId', id); 
    }
  });
  });
Router.map(function() {
    this.route('/post/:_id', function () {
      var item = Posts.findOne({_id: this.params._id});
      this.render('postItem', {data: item});
    },
    {
      name: 'post.show'
    });
});
client/posts/post_item.html

<a href="{{pathFor route='post.show'}}" class="discuss btn">Discuss</a>


如您所见,在
router.js
中,id是从url解析的。另外,我们将
/post/:id
路由命名为'posts.show'之后,我们可以从引用该名称的视图中获取url:
{{pathFor route='post.show'}

它是
这个.params.\u id
哇!这真的奏效了!我用很多不同的类型试了很长时间。。。我想这样做,但它不起作用:
Router.route('/post/:_id',{name:'postPage',data:function(){return Posts.findOne(this.params._id);})
在post_item.html:
和post_page.html
{{{>positem}}
如果我想显示的不仅仅是项目,我需要post_page.html来为单击的项目添加更多细节。我只有一本旧书《发现流星》——我看到了这本书的版本(德语)——好的,谢谢!我稍微测试了一下您的代码,并将其更改为:route.js:
this.route('/post/:_id',function(){var item=Posts.findOne({u id:this.params._id});this.render('postPage',{data:item});},{name:'postPage'})
在post_item.html中讨论链接:
在post_page.html中我可以添加更多细节:
{{>positem}}更多细节在这里!thx浪人