建立並響應釋出上的錯誤

在伺服器上,你可以建立這樣的釋出。this.userId 是當前登入使用者的 ID。如果沒有使用者登入,則可能需要丟擲錯誤並對其進行響應。

import Secrets from '/imports/collections/Secrets';

Meteor.publish('protected_data', function () {
  if (!this.userId) {
    this.error(new Meteor.Error(403, "Not Logged In."));
    this.ready();
  } else {
    return Secrets.find();
  }
});

在客戶端上,你可以使用以下內容進行響應。

Meteor.subscribe('protected_data', {
  onError(err) {
    if (err.error === 403) {
      alert("Looks like you're not logged in");
    }
  },
});

File / imports / collections / Secrets 建立對祕密集合的引用,如下所示:

const Secrets = new Mongo.Collection('secrets');