【问题标题】:Can not read the slug from url on Flow Router (Meteor)无法从流路由器(流星)上的 url 读取 slug
【发布时间】:2015-07-02 23:53:27
【问题描述】:

我正在尝试使用Flow Router 实现基本路由。但是无论我请求什么集合文档的_id;我总是只获得关于我收藏中第一个文档的信息 - “请求”。

所以这是我在文件 /lib/routes.js 中的路由定义:

FlowRouter.route('/requests/:reqId', {  
  subscriptions: function (params, queryParams) {
    this.register('theRequest', Meteor.subscribe('singleRequest', params.reqId));
  },
  action: function (params, queryParams) {
    FlowLayout.render('layout', { aside: 'aside', main: 'singleRequest' });
    console.log("Yeah! We are on the post:", params.reqId);
  },
  name: 'aRequest'
});

这是我的助手:

Template.singleRequest.helpers({  
    getRequest: function () {
        return Requests.findOne();
    }
});

这是我的服务器发布:

Meteor.publish('singleRequest', function (reqId) {  
    return Requests.find({ _id: reqId});
});

这是模板:

<template name="singleRequest">  
  {{#if isSubReady 'theRequest'}}
    {{#with getRequest}}
      <h2>{{_id}}</h2>
      <p>{{reqFrom}}</p>
      <p>{{reqBy}}</p>
    {{/with}}
  {{else}}
    loading...
  {{/if}}
</template> 

我做错了什么? 注意:在控制台中,由于路由定义中的 console.log 命令,我确实看到了正确的 'reqId' slug。但我没有看到它所属文档的相关信息。

谢谢!

【问题讨论】:

    标签: javascript meteor routing router flow


    【解决方案1】:

    好的,我的问题是我之前有另一个订阅,我在其中发布了所有请求 - 而不仅仅是具有特定 _id 的那个。而且因为我没有创建助手来仅获取具有特定_id的助手;当然,服务器刚刚向我发送了第一个请求。

    我的解决方案只是订阅以前的订阅并在帮助程序中定义以获取请求_id:

    FlowRouter.route('/requests/:reqId', {  
      subscriptions: function (params, queryParams) {
        this.register('allRequests', Meteor.subscribe('Requests', params.reqId));
      },
      action: function (params, queryParams) {
        FlowLayout.render('layout', { aside: 'aside', main: 'singleRequest' });
        console.log("Yeah! We are on the post:", params.reqId);
      },
      name: 'aRequest'
    });
    

    和助手:

    Template.singleRequest.helpers({ 
      getRequest: function () {
        var reqId = FlowRouter.getParam("reqId")
        return Requests.findOne({_id:reqId});
      }    
    });
    

    【讨论】:

      【解决方案2】:

      对于浏览此问题并寻找如何让 Flow Router 捕获并动态链接到数据库中的 slug,然后为每个项目调用模板页面的任何人,我做了一个非常简单的示例应用并发布到here on GitHub.

      希望它会帮助某人。

      【讨论】:

        【解决方案3】:

        我相信您的代码对于您正在尝试做的事情是正确的。但是,我从您直接复制 flow-router 中的代码推断出您对 Meteor 还是很陌生。因此,我愿意赌你的应用中仍然有 autopublish 包。

        Autopublish 将集合中的所有数据推送到客户端。即使没有发布/订阅调用。

        你可以做两件事。要保持自动发布(这将使您的开发过程在开始时更容易,但以后可能会更难),只需将您的模板助手更改为:

        Template.singleRequest.helpers({  
            getRequest: function () {
                return Requests.findOne({ _id: FlowRouter.getParam("reqId")});
            }
        });
        

        或者(你最终会想要这样做),进入命令行并输入:

        meteor remove autopublish
        

        您可以在很多地方阅读自动发布的优缺点,然后自行决定哪个选项最适合您。但是,您还应该考虑是否要在将来缓存您的数据(例如使用 subsManager),因此您可能想要更改您的模板助手。

        【讨论】:

        • 感谢您的回复!尽管如此,我已经删除了自动发布包。另外我仍然尝试使用您的代码,但它不起作用:/
        • 嗯。你如何定义你的收藏Requests?您的收藏中有多少文件?如果您在帮助程序期间将 FlowRouter.getParam("reqId") 写入控制台,您会看到什么 - 您正在寻找的 ID(在您的路线中)、第一个文档的 ID 还是什么都没有/null?
        • 你好。其实我已经找到了解决办法。但在此之前 - 你的问题的答案是:是的,我得到了蛞蝓。谢谢邓肯!!!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-03
        • 2016-09-15
        • 1970-01-01
        相关资源
        最近更新 更多