如何在Meteor客户端正确订阅收藏?

时间:2016-09-25 20:27:42

标签: mongodb meteor meteor-blaze



首先,我不是Meteor的新手,但在最新的Meteor更新后我必须重新研究该框架,现在我在客户端使用Meteor订阅时遇到了麻烦。

具体来说,我已经在客户端订阅了一个集合,但是当我尝试引用它时,浏览器控制台报告了错误:

  

模板助手中的异常:ReferenceError:未定义Chatbox

这是我的代码的结构:

<小时/> imports/api/chatbox/chatboxes.js

// define the collection
export const Chatbox = new Mongo.Collection("chatbox");      

<小时/> imports/api/chatbox/server/publication.js - to be imported in server/main.js

import { Meteor } from "meteor/meteor";
import { Chatbox } from "../chatboxes";

Meteor.publish("chatbox", function(parameter) {
    return Chatbox.find(parameter.find, parameter.options);
});

<小时/> imports/ui/chatbox/chatbox.js - page template to be rendered as content upon routing

import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';

import './chatbox.html';
import './chatbox.css';

Template.chatbox.onCreated(function bodyOnCreated() {
    this.state = new ReactiveDict();
    // create subscription query
    var parameters = {
        find: {
            // query selectors
            permission: "1001",
        },
        options: {
            // query options
        }
    };
    Meteor.subscribe("chatbox", parameters);
});

Template.chatbox.helpers({
  canAddMore() {
      // Chatbox collection direct access from client
      return Chatbox.find().count() < 3;     
  },
});

<小时/>
如果你能帮我解决这个问题,我将不胜感激。感谢所有人花时间阅读我的问题!

此致

1 个答案:

答案 0 :(得分:1)

您需要在Chatbox中导入imports/ui/chatbox/chatbox.js

import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { Chatbox } from "../chatboxes"; // correct this path

它现在尚未定义,因为它尚未导入。