在帮助程序之外未定义的客户端上获取流星

Meteor fetch on client undefined outside of helper

我正在尝试获取集合中的一个条目:

client/views/home.js:

criticalCrewNumber = ConfigValues.find({
  name: 'criticalCrewNumber'
}).fetch()[0].value;

但我收到错误消息:

Uncaught TypeError: Cannot read property 'value' of undefined

如果我 运行 浏览器控制台中的代码,所需的值将 return 编辑为字符串。

我尝试过各种方法,例如使用 findOne;将代码放在应用程序的其他位置;使用 iron-router waitOn 进行订阅等。到目前为止,每次尝试都失败了,因为我以 undefined.

结束

集合的定义、发布和订阅方式如下:

lib/config/admin_config.js:

ConfigValues = new Mongo.Collection("configValues");

ConfigValues.attachSchema(new SimpleSchema({
  name: {
    type: String,
    label: "Name",
    max: 200
  },
  value: {
    type: String,
    label: "Value",
    max: 200
  }
}));

both/collections/eventsCollection.js:

if (Meteor.isClient) {
  Meteor.subscribe('events');
  Meteor.subscribe('config');
};

server/lib/collections.js

``` Meteor.publish('events', 函数 () { return Events.find(); });

Meteor.publish('config',函数(){ return ConfigValues.find(); }); ```

有人知道这是怎么回事吗?谢谢。

考虑使用 ReactiveVar (and Meteor.subscribe 回调):

criticalCrewNumber = new ReactiveVar();

Meteor.subscribe('config', {
    onReady: function () {
        var config = ConfigValues.findOne({name: 'criticalCrewNumber'});
        if (config) {
            criticalCrewNumber.set(config.value);
        } else {
            console.error('No config value.');
        }
    },

    onStop: function (error) {
        if (error) {
            console.error(error);
        }
    }
});