0

Im new with meteor.

This is my javascript code
Template.data.helpers({
  data_res: function () {
  //DoChart();
  //var a = "aaaa";
  //return a;
  var a = $.ajax({
   method: "post",
   url: "http://10.102.4.232:124/getdata.asmx/GetCabang",
   data: '',
   contentType: "application/json",
   success: function (data) {
   console.log(data); // 6
  }
  });
  return a;
 }
});
This is my template code 
<template name="data">
{{data_res}}
</template>

the javascript code is to request a web service view ajax and print it in my template. but in my template, the result is [object Object].

my question is how to print the actual value of ajax result in template???

thank you

marc
  • 67
  • 9
  • I'll admit this isn't an __exact__ duplicate because this question is about an ajax call, however the problem and solution are the same: You have an asynchronous call that's being made in a helper which must run synchronously. – David Weldon Aug 24 '15 at 03:04

1 Answers1

1

Because in the Meteor, object would call 'toString' method before template was rendering. So we couldn't print an object in this way, but you could:

// Call your ajax method in this place, or in other place you want
Template.data.onCreated(function(){
  // Using Session to storage ajax response, which will render template automatically when it changed
  Session.setDefault('myObject', 'no-response');
  $.ajax({
    method: "post",
    url: "http://10.102.4.232:124/getdata.asmx/GetCabang",
    data: '',
    contentType: "application/json",
    success: function (data) {
      console.log(data); // 6
      Session.set('myObject', data)
   }
 });
});


Template.data.helpers({
  // When receive ajax response, set it to Session 'myObject', and it would render your template 'data' reactively
  data_res: function () {
    return Session.get('myObject');
  }
});
This is my template code 
<template name="data">  // using '.' to get property in your object
  {{data_res.propertyA}} {{data_res.propertyB}} {{data_res.propertyC}}
</template>

I wish it can help you to solve your problem :-)

Total Shaw
  • 503
  • 5
  • 12