javascript - Meteor fetch on client undefined outside of helper -
i trying fetch entry in collection with:
client/views/home.js:
criticalcrewnumber = configvalues.find({ name: 'criticalcrewnumber' }).fetch()[0].value;
but i'm getting error:
uncaught typeerror: cannot read property 'value' of undefined
if run code in browser console, desired value returned string.
i have tried various things, e.g. using findone
; placing code elsewhere in app; using iron-router waiton
subscription come, etc. every attempt far has failed end undefined
.
here's how collection defined, published , subscribed to:
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', function () { return events.find(); });
meteor.publish('config', function () { return configvalues.find(); }); ```
does know what's going on? thanks.
consider using reactivevar
(and meteor.subscribe
callbacks):
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); } } });
Comments
Post a Comment