2016-11-25 117 views
0

這個流星代碼需要發佈一個文件,其中包含字段plateNum由用戶輸入的匹配值。
爲什麼它不返回任何東西?如何解決它?流星安全訂閱和發佈

//server.publications.js

Meteor.publish('myCol', function (plate) { 
    if (this.userId && plate) { 
    return myCol.find({plateNum: plate}, {fields: {a: 1, b: 1, c: 1, engineSize: 1 }}); 
    } 
}); 

字段「A」,「B」的值,「C」可能不準備在用戶請求的時間,但將通過下式計算後端工人和更新myCol

//client.main.js

Meteor.subscribe('myCol', dict.get('plateNum')); //<== stored info from user 

Template.footer.events({ 
    'click #info':() => { 
    searching = '<span class="note">Searching...</span>'; 
    let plate = document.getElementById('plateNum').value; 
    plate = plate.replace(/\W/g, '').toLowerCase().trim(); // 
    dict.set('plateNum', plate); //<=== store user info here 

    let doc = myCol.findOne({plateNum: plate}); 
    if (!doc || !doc.a) Meteor.call('aaa', plate); 
    if (doc && !doc.b) Meteor.call('bbb', {plateNum: plate},() => {}); 
    if (doc && doc.c && !doc.c) Meteor.call('ccc', {plateNum: plate},() => {}); 
    } 
}); 

回答

0

我想這是因爲當時你的客戶端代碼訂閱myColthis.userId爲空,因此服務器不返回任何內容。

要解決這個問題,你需要確保用戶已經通過認證之前subscribe

更新

這是你可以用它來修復它的代碼:

Template.footer.onCreated(function() { 
    // use this.autorun over Tracker.autorun 
    // so that the code below will be clear on onDestroy event 
    this.autorun(function() { 
    if(Meteor.userId()) { 
     Meteor.subscribe('myCol', dict.get('plateNum')); 
    } 
    }); 
}); 
+0

什麼代碼建議你可以提供這樣做嗎? –

+0

嘿我剛剛更新了答案,如果有任何不清楚的事情只是評論,我會解釋 – Khang