user3768725
user3768725

Reputation:

Passing data from meteor async function client-side

I'm trying to send data from an asynchronous function to an html page. I have successfully used Meteor's wrapAsync and I can log the data array I want in the console. However, the data does not propagate to the html when I return it. When I copy paste the data array from the console into my body.js file, everything works. So there is something about how I am returning the object that is faulty. Any tips appreciated!

Body.js file

import { Meteor } from 'meteor/meteor';

import { Template } from 'meteor/templating';

import { multichainuser } from '../api/permissions.js';

import './body.html';

Template.body.helpers({
  permissions: function(){
    var information = {};
    Meteor.call('mListPermissions', function(err, result){
      console.log(result);
      return result;     
    });
  }, 
});

body.html:

<body>
  <div class="container">
    <header>
      <h1>Permissions</h1>
    </header>

    <ul>
      {{#each permissions}}
        {{> permission}}
      {{/each}}
    </ul>
  </div>
</body>

<template name="permission">
  <li>{{address}}</li>
  <li>{{type}}</li>
</template>

permissions.js

import { Meteor } from 'meteor/meteor';

var multichainuser = require("multichain-node");

Meteor.methods({
  mListPermissions: function() {
    var lPSync=Meteor.wrapAsync(multichainuser.listPermissions, multichainuser);
    var result = lPSync({});
    return result;
  }
});

Upvotes: 1

Views: 62

Answers (1)

Jan Jouke Tjalsma
Jan Jouke Tjalsma

Reputation: 610

There are several ways to fix this. Please take a look at the reactive method package: https://github.com/stubailo/meteor-reactive-method

Another option is to use reactive vars. In the oncreated of the template you create a reactive variable. Then you call the method whos callback updates the reactive var. In the helper all you need to do is get the value from the reactive var.

Upvotes: 2

Related Questions