javascript - How to get JSON data from server storage to client using Meteor.Call and update the return data synchronously in the template? -


i have file named countries.json in root/private folder. want show country names in multi select box within "newcampaignform" template. getcountrylist method on server return json object client.
console log show returned object , sometime print undefined , object.
however, select box not show country list; once on every server restart when console log doesn't print undefined @ first. how solve problem?

reading various qa here, guess have make function synchronous using meteor.wrapasync function find hard write meteor.wrapasync in proper way works. beginner , find hard follow meteor docs lacks easy examples , due confusion callback functions.
if meteor.wrapasync solution here, please re-write code below works. 1st question here. lot!

//client side code    template.newcampaignform.onrendered(function() {        meteor.call('getcountrylist',function(err,result){                                 session.set('cdata',result);                        });  }    template.newcampaignform.helpers({      countrylist:function() {           console.log(session.get('cdata'));        return session.get('cdata');            }        });          // server side code    getcountrylist:function () {      var country = {};     country = json.parse(assets.gettext('countries.json'));   return country;   }      // loop on template    <select data-placeholder="choose country..." class="chosen-select form-control" id="targetcountries" multiple tabindex="1">       {{#each countrylist}}        <option value = "{{name}}" > {{name}} </option>        {{/each}}    </select>

i have created

this simple meteorpad playground

and

this meteorpad playground reactivevar

and this

gist example

as answer.

remaining question:

  1. maybe json not valid array
  2. event rendered not onrendered
  3. when countrylist should array, used wrong naming {{name}} instead of {{this}} on template (this depends bit of json not know yet)

Comments