如果我使用HTTP get,模板中的每个标记都不起作用

时间:2017-05-29 20:29:42

标签: json http templates meteor

我试图从HTTP处理JSON,但它无效。

我的终端为我带来了这个JSON:

[  
   {  
      "data":"1992-04-27 00:00:00",
      "0":"1992-04-27 00:00:00",
      "numeroProntuario":"PAR 08 1377 051",
      "1":"PAR 08 1377 051",
      "nome":"ABDO AZIZ NADER ",
      "2":"ABDO AZIZ NADER ",
      "nascimento":"1933-08-26 00:00:00",
      "3":"1933-08-26 00:00:00",
      "sexo":"M",
      "4":"M",
      "estado_civil":"CASADO",
      "5":"CASADO",
      "filhos":"0",
      "6":"0",
      "local_nascimento":"BEIRUT ",
      "7":"BEIRUT ",
      "rg":"",
      "8":"",
      "cpf":"",
      "9":"",
      "enviado_por":"",
      "10":"",
      "endereco_res":"RUA ANT\u00d4NIO ANDRADE REBELO, 912",
      "11":"RUA ANT\u00d4NIO ANDRADE REBELO, 912",
      "bairro_res":"MORUMBI ",
      "12":"MORUMBI ",
      "cidade_res":"S\u00c3O PAULO",
      "13":"S\u00c3O PAULO",
      "estado_res":"SP",
      "14":"SP",
      "cep_res":"05692000",
      "15":"05692000",
      "fone_res":"",
      "16":"",
      "celular":"",
      "17":"",
      "profissao":"INDUSTRIAL ",
      "18":"INDUSTRIAL ",
      "cargo":"DIRETOR ",
      "19":"DIRETOR ",
      "atividade":"",
      "20":"",
      "empresa":"INTEXTIL ALIL NADER ",
      "21":"INTEXTIL ALIL NADER ",
      "endereco_com":"RUA CONSELHEIRO COTEGIPE, 294",
      "22":"RUA CONSELHEIRO COTEGIPE, 294",
      "cidade_com":"S\u00c3O PAULO",
      "23":"S\u00c3O PAULO",
      "estado_com":"SP",
      "24":"SP",
      "cep_com":"03058000",
      "25":"03058000",
      "fone_com":"",
      "26":"",
      "motivo":"STRESS",
      "27":"STRESS",
      "email":"",
      "28":"",
      "numeroCartao":"",
      "29":"",
      "cod_convenio":"PAR",
      "30":"PAR",
      "codigo_convenio_plano":"116",
      "31":"116",
      "codigo_paciente":"2290",
      "32":"2290"
   }
]

我的剧本是:

Template.listarCampanhas.helpers ({
  paciente : function(){
    try {
      HTTP.get("http://localhost:90/medico/testeAccess.php",null,function( error, response ) {
        return response.data;
      });
    }
    catch(e){
      console.log( "Cannot get paciente data...", e );
    }   
  }
});

在我的模板中,我有:

<template name="listarCampanhas">
  <h3>Campanhas</h3>
  <table class="table table-striped table-hover">
    <thead>
      <tr>
        <th>Nome</th>
        <th>numero Prontuario</th>
      </tr>
    </thead>
    <tbody>
      {{#each paciente}}
      <tr>
        <td>{{nome}}</td>
        <td>{{numeroProntuario}}</td>
      </tr>
      {{/each}}
    </tbody>
  </table>
  <hr>
</template>

有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您没有正确地从函数返回响应。请记住,这是一个异步API请求,将在稍后返回,因此您需要一种方法来反应性地设置变量。

最简单的方法是使用反应变量。如果您还没有包,请运行

meteor add reactive-var

然后,像这样调整代码:

import { ReactiveVar } from 'meteor/reactive-var'

Template.listarCampanhas.onCreated(function() {
  const apiResponse = new ReactiveVar() // initialize a new reactive var

  // this is when we request to the API
  try {
    // this will return later, but it'll set the reactive
    // variable `apiResponse` to whatever the result returns
    HTTP.get("http://localhost:90/medico/testeAccess.php", null, function(error, response) {
      // when returned, set the result to the reactive variable
      return apiResponse.set(response.data)
    })
  }
  catch(e) {
    console.log( "Cannot get paciente data...", e );
  }

  this.data.apiResponse = apiResponse; // bind the reactive var to the template
});

Template.listarCampanhas.helpers ({
  paciente : function(){
    // helpers of a template is always reactively run,
    // i.e. everytime `apiResponse` changes this will get re-run

    return this.apiResponse.get() // return the api response that's set
  }
})

reactive-var是一个必须理解的核心概念,可帮助您使用Meteor构建应用。您可以通过互联网上的谷歌资源来涵盖这一点。

以下是关于该主题的一个很好的阅读:

https://themeteorchef.com/tutorials/reactive-dict-reactive-vars-and-session-variables

答案 1 :(得分:0)

仅返回响应,而不是response.data

HTTP.get("http://localhost:90/medico/testeAccess.php",null,function( error, response ) {
    return response;
  });

由于返回的json中没有response.data

我猜你应该做的是检查返回json的代码,因为它看起来像它的显示方式错误。