0
 private getAllData(): void {
    this.stamm_solvara_jahrService.getAll().subscribe(resp => {
        const keys = resp.headers.keys();
        this.headers = keys.map(key=> `${key}: ${resp.headers.get(key)}`);
        this.stamm_solvara_jahrData = {...resp.body};

        console.log('Data inside method');
        console.log(this.stamm_solvara_jahrData);
        console.log('Header inside method'); 
        console.log(this.headers);
    });
    console.log('Data outside method');
    console.log(this.stamm_solvara_jahrData);
    console.log('Header outside method');
    console.log(this.headers);
}

So inside the subscribe 'process' both instance variables this.stamm_solvara_jahrData and this.headers do have a value. Outside of this they are both empty. How can one solve this?

2 Answers 2

3

this.stamm_solvara_jahrService.getAll() is an asynchronous method, which is why you have to call subscribe on it.

Hence, this.stamm_solvara_jahrData and this.headers are going to be defined when your subscribe callback is executed, but the rest of the code (outside of the subscribe) doesn't wait for that to happen, it will simply try and print those two variables, which will still be undefined.

I would highly recommend reading about asynchronous vs synchronous code to get a better understanding of why this kind of things happen in a language like javascript.

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

Comments

1

IT IS CORRECT because OUTSIDE the subscribe block the variables are EMPTY cause your doing a ASYNC CALL (AJAX) so your piece of code OUTSIDE the subscribe will be execute BEFORE the request is coming back. ONCE the call is back (success or not) the code executed will be the one INSIDE the .subscribe function so the

    this.stamm_solvara_jahrService.getAll().subscribe(resp => {
            const keys = resp.headers.keys();
            this.headers = keys.map(key=> `${key}: ${resp.headers.get(key)}`);
            this.stamm_solvara_jahrData = {...resp.body};

            console.log('Data inside method');
            console.log(this.stamm_solvara_jahrData);
            console.log('Header inside method'); 
            console.log(this.headers);
        }); 

P.S if you want to CATCH also the error response set a new function as second argumento of you subscribe function as:

  this.stamm_solvara_jahrService.getAll().subscribe(resp => {
            const keys = resp.headers.keys();
            this.headers = keys.map(key=> `${key}: ${resp.headers.get(key)}`);
            this.stamm_solvara_jahrData = {...resp.body};

            console.log('Data inside method');
            console.log(this.stamm_solvara_jahrData);
            console.log('Header inside method'); 
            console.log(this.headers);
        }, 
(error)=>{ // <-- this one func catch the error from server
console.log(error)
}); 

Hope this help you!!

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.