0

Estoy intentando pintar el resultado de un json y siempre recibo undefined.

este es el json que recibo :

{antecedentes: Array(1), ultimaconsulta: Array(1), paciente: Array(1)}
antecedentes: Array(1)
0: {idHc: "1", identificacion: "0960205367", creadohc: "2021-06-10 17:01:25", alcohol: "sdfdsf", tabaco: "sdfsdf", …}
length: 1
__proto__: Array(0)
paciente: Array(1)
0: {idPaciente: "101", nombre: "JUan de los Palotes", email: "[email protected]", password: "sdsdweweewf", creadopaciente: "2008-06-07 18:19:43", …}
length: 1
__proto__: Array(0)
ultimaconsulta: Array(1)
0: {idConsultas: "1", fechaConsulta: "2021-05-10 18:16:08", descripcion: "dsdcfas", identificacion: "0960205367", reConsulta: "2021-08-14 18:14:46", …}
length: 1
__proto__: Array(0)
__proto__: Object

mediante esta consulta en el service

getHistoriaPorCedula(termino: string): Observable<General> {
    return this.httpClient.get<General>(
      `${this.baseUrl}/pacientes/gethistoria/${termino}`
    );
  }

en el component.ts tengo este codigo

export class HistoriaClinicaComponent implements OnInit {
  pacientes!: Pacientes;
  isLoading: Boolean = true;

  constructor(
    private pacientesService: PacientesService,
    private activatedRoute: ActivatedRoute
  ) {}

  ngOnInit(): void {
    this.activatedRoute.params.subscribe(({ id }) => {
      this.pacientesService.getHistoriaPorCedula(id).subscribe((general) => {
        this.pacientes = general.paciente;
        this.isLoading = !this.isLoading;
        console.log(general);
        console.log(this.pacientes);
        console.log(this.pacientes.nombre)
      });

    });
  }
}

y en console log de los pacientes recibo esto

[{…}]0: birthday: "1972-05-01"creadopaciente: "2008-06-07 18:19:43"direccion: "casa de la yuca"email: "[email protected]"escolaridad: "inicial"estado: "V"idPaciente: "101"identificacion: "0960205367"nombre: "JUan de los Palotes"ocupacion: "taxista"password: "sdsdweweewf"sexo: "M"telefono: "+000000000000"__proto__: Objectlength: 1__proto__: Array(0)
historia-clinica.component.ts:28 

undefined

el valor undefined corresponde al console.log(this.pacientes.nombre)

cualquier ayuda es bien recibida..

1 respuesta 1

0

Despues de mucho buscar y probar encontre una solución.

Cambie el componente a asi

export class HistoriaClinicaComponent implements OnInit {
  general!: General;
  pacientes!: Paciente;
  antecedentes!: Antecedente;
  ultimaconsulta!: Ultimaconsulta;
  isLoading: Boolean = true;

  constructor(
    private pacientesService: PacientesService,
    private activatedRoute: ActivatedRoute
  ) {}

  ngOnInit(): void {
    this.activatedRoute.params.subscribe(({ id }) => {
      this.pacientesService.getHistoriaPorCedula(id).subscribe(
        (general) => {
          this.utilizarDatos(general);
        },
        (error) => {
          console.log(<any>error);
        }
      );
    });
  }

     
  private utilizarDatos(general: General) {
    this.general = general;
    let { antecedentes, paciente, ultimaconsulta } = this.general;

    paciente.forEach((paciente) => (this.pacientes = paciente));
    antecedentes.forEach((antecedentes) => (this.antecedentes = antecedentes));
    ultimaconsulta.forEach((ultimaconsulta) => (this.ultimaconsulta = ultimaconsulta));

    this.isLoading = !this.isLoading;

  }
}

la interface quedo asi

export interface General {
  antecedentes:   Antecedente[];
  ultimaconsulta: Ultimaconsulta[];
  paciente:       Paciente[];
}

export interface Antecedente {
  idHc:               string;
  identificacion:     string;
  creadohc:           Date;
  alcohol:            string;
  tabaco:             string;
  drogas:             string;
  infusiones:         null;
  alimentacion:       null;
  diuresis:           null;
  sueno:              null;
  infancia:           null;
  respiratorias:      null;
  gastrointestinales: null;
  nefrourologicos:    null;
  neurologicos:       null;
  hematologicos:      null;
  quirurgico:         null;
  alergicos:          null;
  traumatologico:     null;
}

export interface Paciente {
  idPaciente:     string;
  nombre:         string;
  email:          string;
  password:       string;
  creadopaciente: Date;
  identificacion: string;
  ocupacion:      string;
  sexo:           string;
  estado:         string;
  escolaridad:    string;
  birthday:       Date;
  direccion:      string;
  telefono:       string;
}

export interface Ultimaconsulta {
  idConsultas:    string;
  fechaConsulta:  Date;
  descripcion:    string;
  identificacion: string;
  reConsulta:     Date;
  medicamentos:   string;
  metodo:         string;
}

el json recibido

{
  "antecedentes": [
    {
      "idHc": "1",
      "identificacion": "0960205367",
      "creadohc": "2021-06-10 17:01:25",
      "alcohol": "sdfdsf",
      "tabaco": "sdfsdf",
      "drogas": "sdfsdfsdfsdfsd",
      "infusiones": null,
      "alimentacion": null,
      "diuresis": null,
      "sueno": null,
      "infancia": null,
      "respiratorias": null,
      "gastrointestinales": null,
      "nefrourologicos": null,
      "neurologicos": null,
      "hematologicos": null,
      "quirurgico": null,
      "alergicos": null,
      "traumatologico": null
    }
  ],
  "ultimaconsulta": [
    {
      "idConsultas": "1",
      "fechaConsulta": "2021-05-10 18:16:08",
      "descripcion": "dsdcfas",
      "identificacion": "0960205367",
      "reConsulta": "2021-08-14 18:14:46",
      "medicamentos": "ontrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.",
      "metodo": "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. "
    }
  ],
  "paciente": [
    {
      "idPaciente": "101",
      "nombre": "JUan de los Palotes",
      "email": "[email protected]",
      "password": "sdsdweweewf",
      "creadopaciente": "2008-06-07 18:19:43",
      "identificacion": "0960205367",
      "ocupacion": "taxista",
      "sexo": "M",
      "estado": "V",
      "escolaridad": "inicial",
      "birthday": "1972-05-01",
      "direccion": "casa de la yuca",
      "telefono": "+000000000000"
    }
  ]
}

Todo fue resuelto con el uso de .forEach espero que le pueda servir la solución a alguien más.

happy codding!! a todos

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.