4

I am new to angular2 typescript and rxjs and observables .

I am trying to get information from api using this code

let Alertsanswer = this.LogService.getAlertsForClient(this.userId);
    var a = Alertsanswer.subscribe((response) => {
       this.alerts=JSON.parse(response)
console.log(this.alerts) //give the right response.
    },
      error => {
        var err = error;

        alert(err);
      }
    )

as you can see inside the subscribe I initialize a private variable: "this.alerts"

outside the subscribe this variable has undefined value console.log(this.alerts) //give undefined.

Using this.alert in component doom is undefined too this give an error:

<div>{{alerts}}</div>

first question: why ""this.alerts inside subscribe give right value to the console but outside (including the doom) is always undefined.

second question: I understand the the value coming from the server is async, if I have further code that relay on the answer where would I write it(callback), would it be inside the subscribe? what is the meaning of subscribe? thanks for any help

1
  • @Maantu Das are you able to call this "Alertsanswer" subscribe? I am getting undefined error. Commented Apr 9, 2019 at 13:30

2 Answers 2

2

for your first question, this.alerts get the right value after subscribe finish (get the data from api), use this.alerts before it finished will not give the right value. you should use <div *ngIf="alerts">{{alerts}}</div> to make sure the data is ready for use. for example, before subscribe finish, use <div>{{alerts.property}}</div> will be raise an error. console.log is the same reason.

for your second one, use:

this.LogService.getAlertsForClient(this.userId)
.subscribe(response => this.alerts = JSON.parse(response),
()=>console.log('error'),
()=>yourFuncRelayOnTheResponse(this.alerts)); 
Sign up to request clarification or add additional context in comments.

2 Comments

your answer make sense, but just to be sure- what your are basically say is that ngif inside the doom is listening to changes in the data?
I mean use ngIf to make sure data is ready for use in html, without the ngIf the data also change when you refresh the page.
0

Hey you are doing some wonky stuff by assigning services into variables. Have edited you code a bit to show you how I would define things:

export class LoginComponent
{
    private alerts; //This needs to be defined here on the class first

    constructor(public loginService: LoginService)
    {
    }

    ngOnInit()
    {
        this.LogService
            .getAlertsForClient(this.userId)
            .subscribe(response =>
                {
                    this.alerts = JSON.parse(response); // Assign response to class member alerts.
                },
                error=>
                {
                    console.log(error);
                })
    }
} 

2 Comments

your code still give me undefined in the doom.is it possible that the doom is print before observable finish bringing the data?
Also api calls are asynchronous, so your component view will have access to the alerts property before it has been populated by response from the api. Durring this stage it will be undefined so will throw errors. To get round this you will have to add an ngIf to any bit of markup that uses the alerts property. @LFJ explains this by doing <div *ngIf="alerts">{{alert}}</div>

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.