have made error in code or have found bug in autoform or meteor? believe error cannot find it.
in meteor application when querying collection provide values select input field in form, function run on server returns correctly populated array empty array when run on client.
the application track medical research subjects. have collections studies , study sponsors. when creating or editing study want enter study sponsor list of existing sponsors. autoform , simple-schema used create create , edit forms. select input study sponsor names supplied names in schema studies array given 'allowedvalues'. when allowedvalues given explicit array works expected. however, when array supplied function
sponsornames = function(){ sn = sponsors.find().map(function(sp){ return sp.sponsorname }); console.log(sn); return sn; }; that collects array values sponsors collection generated list empty.
the console.log statement in sponsornames function prints populated array cli running application. however, browser console shows empty array same console.log statement. believe code running on both server , on client yielding 2 different results. in other respects code runs should.
an abbreviated application structure:
research both collections sponsors.js studies.js router routes.js client sponsor events.js helpers.js templates.html study events.js helpers.js templates.html subscribe.js server methods.js publish.js security.js both/collections/sponsors.js
sponsors = new meteor.collection('sponsors'); schema.sponsors = new simpleschema({ sponsorname: { type: string, label: 'sponsor name' }, }); sponsors.attachschema(schema.sponsors); both/collections/studies.js
studies = new meteor.collection('studies'); sponsornames = function(){ sn = sponsors.find().map(function(sp){ return sp.sponsorname }); console.log(sn); return sn; }; schema.studies = new simpleschema({ studyname: { type: string, label: 'study name' }, sponsor: { type: string, label: 'sponsor', allowedvalues: sponsornames(), }, sitenum: { type: string, label: 'site number' }, }); studies.attachschema(schema.studies); client/study/templates.js
<template name='editstudy'> {{#autoform collection=studies id="updatestudyform" type="update" doc=doc}} <fieldset> <legend>edit study</legend> {{> studypanel1}} </fieldset> <button type="submit" class="btn btn-primary">update</button> {{/autoform}} </template> <template name='studypanel1'> {{> afquickfield name="studyname" class="form-control input"}} {{> afquickfield name="sponsor" class="form-control input" options='allowed' }} {{> afquickfield name="sitenum" class="form-control input"}} </template> client/study/helpers.js
template.addstudy.helpers({ studies: function(){ return studies; }, }); template.editstudy.helpers({ studies: function(){ return studies; }, doc: function(){ return this; } }); client/study/events.js
var sponsorhooksobject = { after: { insert: function(error, result) { if (!error) { router.go('sponsorspage'); }; }, update: function(error, result) { if (!error) { router.go('sponsorspage'); }; } }, }; autoform.hooks({ insertsponsorform: sponsorhooksobject, updatesponsorform: sponsorhooksobject }); client/subscribe.js
meteor.subscribe('subjects'); meteor.subscribe('studies'); meteor.subscribe('sponsors'); server/methods.js
meteor.methods({ 'removesubjectdata': function(id){ subjects.remove(id); }, 'removestudydata': function(id){ studies.remove(id); }, 'removesponsordata': function(id){ sponsors.remove(id); }, }); server/publish.js
meteor.publish('subjects', function(){ return subjects.find({}); }); meteor.publish('studies', function(){ return studies.find({}); }); meteor.publish('sponsors', function(){ return sponsors.find({}); }); server/security.js
subjects.permit(['insert', 'update', 'remove']).apply(); studies.permit(['insert', 'update', 'remove']).apply(); sponsors.permit(['insert', 'update', 'remove']).apply(); meteor list:
accounts-password 1.1.1 alanning:roles 1.2.13 aldeed:autoform 5.3.2 aldeed:collection2 2.3.3 aldeed:simple-schema 1.3.3 aslagle:reactive-table 0.8.9 email 1.0.6 fortawesome:fontawesome 4.3.0 ian:accounts-ui-bootstrap-3 1.2.71 iron:router 1.0.9 meteor-platform 1.2.2 ongoworks:security 1.2.0 reactive-var 1.0.5 twbs:bootstrap 3.3.5
i going wrong. query of sponsors collection sponsornames populate select field belongs in template helper. these changes app works correctly.
both/collections/studies.js
studies = new meteor.collection('studies'); // remove these: // sponsornames = function(){ // sn = sponsors.find().map(function(sp){ return sp.sponsorname }); // console.log(sn); // return sn; // }; schema.studies = new simpleschema({ studyname: { type: string, label: 'study name' }, sponsor: { type: string, label: 'sponsor', // remove this: // allowedvalues: sponsornames(), }, sitenum: { type: string, label: 'site number' }, }); studies.attachschema(schema.studies); client/study/templates.js
<template name='editstudy'> {{#autoform collection=studies id="updatestudyform" type="update" doc=doc}} <fieldset> <legend>edit study</legend> {{> studypanel1}} </fieldset> <button type="submit" class="btn btn-primary">update</button> {{/autoform}} </template> <template name='studypanel1'> {{> afquickfield name="studyname" class="form-control input"}} {{> afquickfield name="sponsor" class="form-control input" type='select' options=sponsornames }} <!-- add this: --> {{> afquickfield name="sitenum" class="form-control input"}} </template> client/study/helpers.js
template.addstudy.helpers({ studies: function(){ return studies; }, }); template.editstudy.helpers({ studies: function(){ return studies; }, doc: function(){ return this; } }); // add this: template.registerhelper("sponsornames", function() { return sponsors.find().map(function(sp){ return {label: sp.sponsorname, value: sp.sponsorname}; }); });
Comments
Post a Comment