I'm struggling with an issue using Meteor JS.
I call an API which return me a JSON array which look like the one returned on this URL (I don't put the whole array here cause of the size): https://blockchain.info/address/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX?format=json&offset=0
I call it server side like:
if (Meteor.isServer) {
Meteor.methods({
getWalletPreviousTx: function() {
var url = "https://blockchain.info/address/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX?format=json&offset=0";
var result = Meteor.http.get(url);
if(result.statusCode==200) {
var tx = JSON.parse(result.content);
return tx;
} else {
console.log("Response issue: ", result.statusCode);
var errorJson = JSON.parse(result.content);
throwError("Couldn't fetch wallet balance from Blockchain, try again later !");
}
}
});
}
And I retrieve it to my view via an helper in a specific template:
Template.wallet.helpers({
addrTxs: function () {
Meteor.call('getWalletPreviousTx', function(err, tx) {
console.log(tx);
return [tx];
});
}
});
The console.log in the helper actually logs my JSON array which means it has access to it. Now the part I'm struggling with is to retrieve this JSON to my view, I've tried a lot of way and none of them works, actually I have this in my view:
<template name="wallet">
<table>
{{#each addrTxs}}
<ul>
{{> addrTx}}
</ul>
{{/each }}
</table>
</template>
The part of the JSON I want to display is the "addr" and "value" of each transactions:
"inputs":[
{
"sequence":4294967295,
"prev_out":{
"spent":true,
"tx_index":97744124,
"type":0,
"addr":"1AWAsn8rhT555RmbMDXXqzrCscPJ5is5ja",
"value":50000,
"n":0,
"script":"76a914683d704735fd591ba9f9aebef27c6ef00cbd857188ac"
}
}
]
Fact is, I never managed to display anything from this JSON array in my view, even putting directly this in my view doesn't show anything:
{{addrTxs}}
What am I doing wrong? Can anyone help with this?
Thanks for reading.
----------------------- Edit ---------------------
I think the problem is more that my helper and template are loaded before the API call is finished (because the console.log appear in my console like 3 seconds after my page is rendered). How can I make my helper wait until the API call is finished before rendering it in the view? I use iron router.
I have tried to add a waitOn action on my route in order to wait until my API call is finished:
Router.route('/wallet', {
name: 'wallet',
template: 'wallet',
loadingTemplate: 'loading',
waitOn: function () {
Meteor.call('getWalletPreviousTx', function(error, result) {
if(!error) {
Ready.set(result)
}
});
return [
function () { return Ready.get(); }
];
},
action: function () {
if (this.ready())
this.render();
else
this.render('loading');
}
});
The above code with the waitOn action seems to work (I have no errors) but I don't know the way to display in my view the specific result from:
if(!error) {
Ready.set(result)
}