简体   繁体   English

如何在Meteor中订阅收藏

[英]How to subscribe in Meteor to a collection

I have a problem I removed autopublish and now I want to subscribe again to my collections but it does not work ;( 我有一个问题,我删除了自动发布,现在我想再次订阅我的收藏集,但是它不起作用;(

Here is my lib code for the two collections: 这是我的两个集合的lib代码:

Events = new Mongo.Collection('events');
Friends = new Mongo.Collection('friends');

then my server main.js 然后我的服务器main.js

import { Meteor } from 'meteor/meteor';

Meteor.startup(() => {
  // code to run on server at startup



});

if (Meteor.isServer) {
  Meteor.publish("events", function() {
      return events.find();
  })
  Meteor.publish("friends", function() {
      return friends.find();
  })
}

and last my client code in the main.js on the client side 最后我的客户端代码在客户端的main.js中

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';

import './main.html';

Meteor.subscribe("events");
Meteor.subscribe("friends");

I am running Meteor 1.4 and my problem is I cant get information from the collection 我正在运行Meteor 1.4,但我的问题是我无法从集合中获取信息

Here is some code that with the autopublish already worked 这是一些可以自动发布的代码

Template.NeuesEvent.events({
    "submit .add-event": function(event){
        var Name = event.target.name.value;
        var Beschreibung = event.target.beschreibung.value;
        var Datum = event.target.Datum.value;
        var Autor = Meteor.userId();
        var eingeladene = [];
             $.each($('.FreundeCheckbox:checked'), function(){            
                eingeladene.push($(this).val());
            });


        if (Name == "")
        {
            confirm("Das Event braucht einen Namen ;)")
        }
        else {

        Events.insert({
            Name: Name,
            Beschreibung: Beschreibung,
            erstelltAm: new Date(),
            Datum: Datum,
            Eingeladen: eingeladene,
            Autor: Autor
        });

        event.target.name.value = "";
        event.target.beschreibung.value = "";
        FlowRouter.go('/meineEvents');

        return false;
    }
    }
});

and the helper to "print" it out 和帮手“打印”出来

Template.meineEvents.helpers({
    event: function(){
        return Events.find({}, {sort: {createdAt: -1}});
    }
});

Hope you can help me to modify it so it works again Thanks ;) 希望你能帮我修改它,使其再次起作用谢谢;)

you have to reference the symbols, not the collection names, like this: 您必须引用符号,而不是集合名称,如下所示:

if (Meteor.isServer) {
  Meteor.publish("events", function() {
      return Events.find();
  })
  Meteor.publish("friends", function() {
      return Friends.find();
  })
}

edit: 编辑:

you said you have these collections: 您说您有以下收藏:

Events = new Mongo.Collection('events');
Friends = new Mongo.Collection('friends');

where are they defined? 他们在哪里定义? are they available to both client and server? 它们对客户端和服务器都可用吗? are they under any folder called "/imports"? 它们在任何名为“ / imports”的文件夹下吗? if so, then they are not eagerly loaded and you need to export/import those symbols. 如果是这样,则不会急于加载它们,您需要导出/导入这些符号。

edit 2: 编辑2:

the next thing to check is to see if the data is published. 接下来要检查的是查看数据是否已发布。 the easiest way to do that is install meteortoys:allthings ( https://atmospherejs.com/meteortoys/allthings ) and see if the data made its way to the client. 最简单的方法是安装meteortoys:allthings( https://atmospherejs.com/meteortoys/allthings ),然后查看数据是否已进入客户端。 if not, then i would check to ensure that the subscribe() lines you provided are associated with the NeuesEvent template. 如果没有,那么我将检查以确保您提供的subscribe()行与NeuesEvent模板相关联。

i typically do my subscriptions in the template, like this: 我通常在模板中进行订阅,如下所示:

Template.NeuesEvent.onCreated(function() {
    this.subscribe('events');
    this.subscribe('friends');
});

... but it depends on your use case. ...但这取决于您的用例。 here, those subscriptions will be active as long as the NeuesEvent template is active. 在这里,只要NeuesEvent模板处于活动状态,这些订阅就会处于活动状态。 when you switch to a different template, the subscriptions will end. 当您切换到其他模板时,订阅将结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM