2016-09-25 95 views
1




首先,我不是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;  
    }, 
}); 



我會很感激,如果你能幫助我這個問題。感謝所有花時間閱讀我的問題!

問候

回答

1

您需要在imports/ui/chatbox/chatbox.js進口Chatbox

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

它,因爲現在它尚未導入是不確定的。

+0

我寫這些代碼的意圖是我希望客戶端只有他/她有權查看的集合的子集,但是從我所瞭解的情況來看,如果我將Chatbox導入到chatbox.js中,從「../ chatboxes」導入{Chatbox};',整個集合將被客戶端訪問。如果我的理解錯誤,請糾正我,謝謝! – mrawesome

+0

這不是它的工作原理。控制客戶端訪問的集合子集的方法是通過Meteor發佈和訂閱。除非服務器發佈數據和客戶端訂閱,否則在客戶端導入集合將不會添加任何服務器數據。 Meteor文檔和指南深入介紹pub/sub,因此這些都是很好的參考點。 –

+0

如果你還沒有通過流星教程,你應該。控制要發佈的數據的一種方法是通過篩選。像這樣:return Chatbox.find({_ id:「someuserid」});在發佈功能裏面。 – ickyrr