【问题标题】:How to subscribe to certain records in collection with id in meteor如何使用流星中的 id 订阅集合中的某些记录
【发布时间】:2015-11-13 20:56:40
【问题描述】:

我是流星新手,我有两个名为 Employee 和 Visitor 的集合。还有两个模板 Home 和 Print。我可以像这样从按钮获取记录的 ID。

printVisitor:function(){
    return Visitor.findOne({_id:Session.get('UpdateVisitorId')});
}

现在,当我单击重定向到另一个页面的按钮时,我需要使用可以从上述代码中获得的特定记录 ID 打印这些值,例如姓名、电话号码。 我的路线代码如下所示

Router.route('/print/:_id', {

名称:'打印', 控制器:'打印控制器', 行动:'行动', 其中:“客户” });

我的打印 html 是这样的

<template name="Print">

This is: {{VisitorName}}
Visiting:{{EmployeeName}}
Phone:{{PhoneNumber}}

我如何发布、订阅和打印该 id 的特定值

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    发布和订阅可以带参数。您还想使用this.params 来获取 URL 参数。这是一般模式

    Router.route('/foo/:_id', {
    
        name: 'foo',
        waitOn: function(){
          // this grabs the ID from the URL
          var someId = this.params._id;
          // this subscribes to myPublication while sending in the ID as a parameter to the publication function
          return [
            Meteor.subscribe('myPublication', someId)
          ]
        }
    
    });
    
    
    /server/publications.js
    
    Meteor.publish('myPublication', function(id){
      check(id, String);
      return MyCollection.findOne({_id: id});
    });
    

    您现在可以访问您在此路线上订阅的数据。

    【讨论】:

    • 我已经按照你说的做了。现在我得到的只是在 html 中“加载.....”。我的代码有什么问题吗?
    • @fuzzybabbybunny 你好,伙计,即使我尝试复制此代码,但我看到了类似的错误:我能看到的是 sacha 的加载微调器工作。如果你能检查出来会很高兴。
    • 加载屏幕意味着客户端正在等待来自服务器的数据。如果它没有停止加载,则表示服务器向客户端发送数据时出现问题 - 在这种情况下,我忘记将 check() 放在发布函数中。 Meteor 要求在使用之前检查从客户端发送到服务器的所有参数。您应该在 Meteor 服务器控制台中看到指向此的错误。
    • 看准了,伙计!现在就像一个魅力。谢谢!
    • 你需要把return MyCollection.findOne({_id: id});改成return MyCollection.find({_id: id});,否则你会在服务器端得到Error: Publish function can only return a Cursor or an array of Cursors
    猜你喜欢
    • 1970-01-01
    • 2015-09-23
    • 2016-01-23
    • 2017-02-14
    • 1970-01-01
    • 2013-03-18
    • 2013-02-15
    • 2020-11-18
    • 1970-01-01
    相关资源
    最近更新 更多