Data Return with Angular

Asked

Viewed 235 times

0

I have a service with the following structure:

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import 'rxjs-compat/add/operator/map';
import {map} from 'rxjs/operators';

@Injectable()
export class BlockchainService {
    constructor(private http: HttpClient) { }
    url = 'https://blockchain.info/q/getdifficulty';

    getDifficulty() {
        return this.http.get(this.url)
            .pipe(map(res => res)) // pega os dados sempre q estiver disponível e remapeia
            .subscribe(data  => data, error => console.log('erro')); // subscribe ouve um observable
    }
}

And I’m calling it that

import {Component} from '@angular/core';
import {BlockchainService} from '../blockchain.service';
import {Subscriber, Subscription} from 'rxjs';
import * as util from 'util';

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
  dificuldade: Subscription;
  constructor(blockchainService: BlockchainService) {
   this.dificuldade = blockchainService.getDifficulty();
  }


}

The problem is that on the front, when calling with interpolation {{dificuldade}}, only returns [object Object] and, when inspecting, it has the following structure, which does not contemplate the data it should receive

{ closed: false, _parentOrParents: null, _subscriptions: [ { closed: false, _parentOrParents: [Circular], _subscriptions: [Object], syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: true, isStopped: false, destination: [Circular], project: [Function], count: 0, thisArg: [Circular] } ], syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: true, isStopped: false, destination: { closed: false, _parentOrParents: null, _subscriptions: null, syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: false, isStopped: false, destination: { closed: true, next: [Function: next], error: [Function: error], complete: [Function: complete] }, _parentSubscriber: [Circular], _context: [Circular], _next: [Function], _error: [Function], _complete: undefined } }

How should I get this data correctly? If I pass one console.log in the first subscribe parameter, I can print the desired way in the console

  • What are you waiting for as url return ? What kind of data ? A json ?

  • A string. The api opens, you can see: https://blockchain.info/q/getdifficulty

1 answer

1


Try it this way:

{{dificuldade|async}}

if pq difficulty is an observable and not your data itself.

  • Yeah, it didn’t work

  • Takes the subscribe from your service

  • I’ve already taken and passed to Component, but I still can’t access the value directly to set in the variable "difficulty"

  • I figured out what the problem was, "this.init();" was missing from the constructor. Silly thing, but it was a bit of a hit. Thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.