Возвращает данные как Null после вызова метода подписки httpget из компонента - PullRequest
0 голосов
/ 06 июля 2019

Вот мой файл компонента:

import { Component, OnInit } from '@angular/core';
import { CclcService } from '../cclc.service';
import {HttpClient,HttpHeaderResponse,HttpClientModule} from '@angular/common/http';
import { ICurrencyData } from './ICurrencyData';
import { Currency } from './Currency';

@Component({
  selector: 'app-cryto-currencies-list',
  templateUrl: './cryto-currencies-list.component.html',
  styleUrls: ['./cryto-currencies-list.component.css']
})
export class CrytoCurrenciesListComponent implements OnInit {
  constructor(private cclcService: CclcService ) {
      console.log('cclc component called');
   }

   public list : Currency;

   ngOnInit() {    
      this.getCurrencyList();
   }

   public getCurrencyList() 
   {
       this.cclcService.getCryptoCurrencyList().subscribe(data => {
         console.log(data);
         this.list = data;
         console.log(this.list );
       })
   }
}

import { Injectable } from '@angular/core';
import {HttpClient,HttpHeaderResponse,HttpClientModule,HttpErrorResponse} from '@angular/common/http';
import { Observable } from "rxjs";
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot,ActivatedRoute } from '@angular/router';
import { ICurrencyData } from './cryto-currencies-list/ICurrencyData';
import { Currency } from './cryto-currencies-list/Currency';


@Injectable({
  providedIn: 'root'

})

export class CclcService {
  private list : ICurrencyData[];

  constructor(private httpClient:HttpClient ,private data:ICurrencyData,private resp: HttpHeaderResponse,private route:Router,actRoute:ActivatedRoute) {
    console.log('cclc service constructor called');
    resp.headers.set('Access-Control-Allow-Origin', '*');
   }
   private baseUrl = 'https://pro-api.coinmarketcap.com/';
   private apiKey= 'bf8bbb9e-a227-46cb-862d-53a9ad3bbe51';

   public getCryptoCurrencyList() : Observable<Currency>
   {
     return this.httpClient.get<Currency>(this.baseUrl + 
      'v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=' + this.apiKey)
      console.log('service');          
   }
}

Я звоню через web api get через службу, используя модуль клиента http. Но данные возвращаются как нулевые после вызова метода подписки из компонента. Он прекрасно работал, когда запрос вызывался почтальоном.

В идеале должен возвращать результат json.

Но получение данных как нулевых при успешном завершении запроса на подписку

cclc service constructor called
cryto-currencies-list.component.ts:18 cclc component called
cclc.service.ts:30 service
cclc.service.ts:31 Observable {_isScalar: false, source: Observable, operator: MapOperator}
cryto-currencies-list.component.ts:28 null
cryto-currencies-list.component.ts:30 null
...