2

I´m retrieving a String from backend:

String value = "{I am here}";

On my service:

getValue(): Observable<String> {
    return this.http.get<String>(this.myURL);
  }

On my component I´m subscribing:

String myMessage;

  getValue(): void {
    this.myService.getValue()
    .subscribe(message=> {
        this.myMessage= myMessage;
        console.log("WHY NOT ENTER HERE?)

);
  });

What I´m doing wrong for not even getting the message from console.log?

2
  • Can be any error. Add error block to your subscription and console in there to see the error. Commented Sep 26, 2019 at 23:01
  • 3
    btw, a bit irrelevant here, but there's a difference in string and String. Commented Sep 26, 2019 at 23:01

2 Answers 2

5

I guess you need to be specific about the responseType if you want to use string.

Here is the doc https://angular.io/guide/http#requesting-non-json-data

In your case, the getValueshould do this:

return this.http.get(this.myURL, { responseType: 'text' });

Note that you don't need to use <string>

Sign up to request clarification or add additional context in comments.

Comments

0

From what I see, there is a mistake in your code, probably the reason you can't assign any value to myMessage:

Your code:

String myMessage;

  getValue(): void {
    this.myService.getValue()
    .subscribe(message=> {
        this.myMessage= myMessage;
        console.log("WHY NOT ENTER HERE?)

);
  });

It should be:

String myMessage;

  getValue(): void {
    this.myService.getValue()
    .subscribe(message=> {
        this.myMessage= message;
        console.log("WHY NOT ENTER HERE?)

);
  });

Notice the value assignment part in the callback function, you've used the wrong variable.

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.