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

Meteor fetch on client undefined outside of helper

本文关键字:获取 流星 客户端 帮助程序 外部 未定义      更新时间:2023-09-26

我正在尝试使用以下方法获取集合中的条目:

客户/视图/主页.js

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

但是我收到错误:

Uncaught TypeError: Cannot read property 'value' of undefined

如果我在浏览器控制台中运行代码,则所需的值将以字符串形式返回。

我已经尝试了各种方法,例如使用findOne;将代码放在应用程序的其他位置;使用铁路由器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');
};

服务器/库/集合.js

'

''Meteor.publish('events', function () { 返回 Events.find(); });

Meteor.publish('config', function () { 返回 ConfigValues.find(); });```

有谁知道发生了什么?谢谢。

考虑使用 ReactiveVar(和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);
        }
    }
});