2017-05-29 3 views
0

Ich versuche, JSON von HTTP zu verarbeiten, aber es funktioniert nicht.Jedes Tag in Vorlage funktioniert nicht, wenn ich HTTP verwende

Mein Endpunkt mir dieses JSON bringen:

[ 
    { 
     "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" 
    } 
] 

Und mein Skript ist:

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); 
    } 
    } 
}); 

In meiner Vorlage habe ich:

<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> 

niemanden wissen, wie dieses Problem zu beheben ?

+0

Versuchen Sie es mit 'JSON.parse' an diesem response.data oder wo auch immer Ihre Antwort ist. –

Antwort

0

Return gerade Reaktion statt response.data heißt

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

Da es keine response.data in der zurück json ist.

Ich rate, was Sie wirklich tun sollten, ist der Code, der die JSON zurückgibt, wie es falsch aussieht wie die Art, wie es angezeigt wird.

+0

nicht funktionieren, aber wenn ich Json Antwort setzen manuell wie folgt aus: return [{ "Daten": "1992.04.27 00:00:00", \t \t \t \t \t "numeroProntuario": "PAR 08 1377 051" , \t \t \t \t \t "nome": "ABDO AZIZ NADER" \t \t \t \t \t \t \t \t \t \t}, \t \t \t \t \t { "Daten": "1992.04.27 00:00:00", \t \t \t \t \t "numeroProntuario": "08 PAR 1377 051", \t \t \t \t \t "nome": "ABDO AZIZ NADER " \t \t \t \t \t \t \t \t \t \t}, \t \t \t \t \t { "Daten": "1992.04.27 00:00:00", \t \t \t \t \t "numeroProntuario": "PAR 08 1377 051", \t \t \t \t \t "nome": "ABDO AZIZ NADER " \t \t \t \t \t \t \t \t \t \t}, {" data ":" 1992.04.27 00:00:00" , \t \t \t \t \t „n umeroProntuario ":" PAR 08 1377 051" , \t \t \t \t \t "nome": "ABDO AZIZ NADER" \t \t \t \t \t \t \t \t \t \t}]; dann arbeite –

+0

Das ist, was ich gesagt habe, dein Endpunkt gibt das json in der Struktur nicht zurück, die du benötigst. – gaganshera

+0

Das ist nicht das Problem hier. Die Verwendung von 'HTTP.get' hat immer einen' data' Schlüssel in der Antwort. Siehe https://docs.meteor.com/api/http.html –

1

Sie geben die Antwort der Funktion nicht korrekt zurück. Denken Sie daran, dass es sich um eine asynchrone API-Anfrage handelt, die zu einem späteren Zeitpunkt zurückgegeben wird, sodass Sie die Variable reaktiv einstellen müssen.

Der einfachste Weg dazu ist die Verwendung reaktiver Variablen. Wenn Sie das Paket nicht bereits haben, laufen

meteor add reactive-var 

Dann Anpassungen vornehmen, um Ihren Code wie folgt:

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 ist ein Kern, Konzept muss verständlich, die Ihnen helfen werden in Aufbau einer App mit Meteor. Sie können Ressourcen im Internet googlen, die dies abdecken.

Hier ist ein gutes über das Thema lesen:

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

Verwandte Themen