Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
Angular call to REST API not waiting for the data to arrived

I have the following code:

export class Whatever implements OnInitn{

    prices;

     ngOnInit(){
       this.CoinPricesService.getPrices().subscribe(
           pricesFromResponse => {
             this.prices = pricesFromResponse;
              console.log('this is what I get from the api: ', this.prices);
           }
       );
       
      console.log('this is my prices class object', this.prices);
     }
}

the output is this:

this is my prices class object undefined

api: this is what I get from the $, $ $

as you can see the first message is showing in second and the second is in the first place with an UNDEFINED, obviously what is happening is that the code is not waiting for the response to come, is being sychronous, WHY ? isn't observable.Subscribe(response ==> this.myVar = response); meant to take care of that ? I can't use the data in other places of my class because it's always undefined when the codes hits there, please help

My Service Looks like this:

@Injectable({
  providedIn: 'root'
})
export class CoinPricesService {

  private apiURl = 'https://api.forprices/blablabla';

  constructor(private http: HttpClient) { }

  getPrices(){
    return this.http.get<any>(this.apiURl);
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you had a function that depended on price, then you could execute the functions within the subscribe. something like this:

    export class Whatever implements OnInitn{
    
        prices;
    
         ngOnInit(){
           this.CoinPricesService.getPrices().subscribe(
               pricesFromResponse => {
                 this.prices = pricesFromResponse;
                  console.log('this is what I get from the api: ', this.prices);
                  someFunction(pricesFromResponse)
               }
           );
           
          
         }
         someFunction(prices){//use price ...} 
    } 
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda