2015-07-16 3 views
0

шаблона помощник:JSON объект. Не удается получить данные в шаблоне. Метеор

Template.userLoggedIn.helpers({ 
    stream: function() { 
     return ReactiveMethod.call("checkApi", function(err, results) { 
      console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); //nothing 
      res = JSON.parse(results.content); // nothing 
      return res 
     }); 
    } 

В моем шаблоне:

{{#with stream }} 
    {{this}} //Object object 
{{/with}} 

Если я пытаюсь:

{{#with stream }} 
     {{this.livestream}} //Undefined 
    {{/with}} 

Мой Meteor.Method:

Meteor.methods({ 
     checkApi: function() { 
      this.unblock(); 
      re = Meteor.http.call("GET", "http://api.hitbox.tv/media/live/uccleague") 
      return re; 
     } 
    }); 

Как я n получить доступ к этому объекту json? Возвращения метода вызова (JSON) здесь: http://www.jsoneditoronline.org/?id=1ae1eaaf7a4e942bd4a0728517b10779

ответ

1

ReactiveMethod не принимает функцию обратного вызова в качестве последнего аргумента (избегая обратный вызов является всей точкой пакета). Вы должны использовать его как это:

Template.userLoggedIn.helpers({ 
    stream: function() { 
    return ReactiveMethod.call('checkApi'); 
    } 
}); 

Что означает, что это нужно делать синтаксический анализ JSON в методе примерно так:

Meteor.methods({ 
    checkApi: function() { 
    this.unblock(); 
    try { 
     var result = Meteor.http.call('GET', '...'); 
     return JSON.parse(result.content); 
    } catch (e) { 
     return ''; 
    } 
}); 
Смежные вопросы