如何正确订阅流星客户端的集合?
问题描述:
首先,我不是Meteor的新手,但是在最新的Meteor更新之后,我不得不重新研究这个框架,现在我在客户端使用Meteor订阅时遇到了麻烦。
具体而言,我已经订阅的集合在客户端,但是当我尝试引用它的浏览器控制台报告错误:如何正确订阅流星客户端的集合?
Exception in template helper: ReferenceError: Chatbox is not defined
这里是我的代码的结构:
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;
},
});
我会很感激,如果你能帮助我这个问题。感谢所有花时间阅读我的问题!
问候
答
您需要在imports/ui/chatbox/chatbox.js
进口Chatbox
:
import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { Chatbox } from "../chatboxes"; // correct this path
它,因为现在它尚未导入是不确定的。
我写这些代码的意图是我希望客户端只有他/她有权查看的集合的子集,但是从我所了解的情况来看,如果我将Chatbox导入到chatbox.js中,从“../ chatboxes”导入{Chatbox};',整个集合将被客户端访问。如果我的理解错误,请纠正我,谢谢! – mrawesome
这不是它的工作原理。控制客户端访问的集合子集的方法是通过Meteor发布和订阅。除非服务器发布数据和客户端订阅,否则在客户端导入集合将不会添加任何服务器数据。 Meteor文档和指南深入介绍pub/sub,因此这些都是很好的参考点。 –
如果你还没有通过流星教程,你应该。控制要发布的数据的一种方法是通过筛选。像这样:return Chatbox.find({_ id:“someuserid”});在发布功能里面。 – ickyrr