javascript - Populate html table with http API request in Meteor -


i have table

<table>  <thead>   <tr>    <th>year</th>    <th>value</th>   </tr>  </thead>  <tbody>   <tr>    <td>1990</td>    <td>-</td>   </tr>   ...  </tbody> </table> 

and want make request world bank api fill out values in table.

my request in meteor method

meteor.methods({   getdata: function () {     try {       const url = 'http://api.worldbank.org/countries/br/indicators/ny.gdp.mktp.cd?date=1990:2000&format=json';       const request = http.get(url);       return request;     } catch (e) {       console.log(e);       return false;     }   }, }); 

and call method client with

meteor.call('getdata', function(error, result) {   console.log(result); }); 

so values in console, want replace - in html table correct values.

you use {{#each}} block helper loop through array. e.g.

<tbody>  {{#each getdata}}  <tr>   <td>{{date}}</td>   <td>{{value}}</td>  </tr>  {{/each}} </tbody> 

where getdata template helper fetches data. however, because data you're getting async, cannot meteor.call() directly in helper.

one way solve session variable ('meteor add session' install package).

first, you'd fetch data when creating template , set session variable:

template.your_template.oncreated(function() {     meteor.call('getdata', function(error, result) {         // result.data[1] array of objects data         session.set('bankdata', result.data[1]);     }); }); 

then, use template helper pass data template:

template.your_template.helpers({     getdata: function(){                 return session.get('bankdata');     } }); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -