1

am getting the details from a service and assigning it to an loginDetailsArray array. now i need to call the functions() only after getting the data successful. i just tried to call functions() directly, but it is reading that function before getting the data. i was thinking about using call back function here. but as am new to this, i have no idea of how to get it. Thanks in advance.

loadLoginDetails() {

  this.customerService.getLoginDetails().subscribe(res => {

    this.loginDetailsArray = res.json();
  });
}


functions() {

    var minLength = this.loginDetailsArray.rules.username.minlength;
    var maxLength = this.loginDetailsArray.rules.username.maxlength;
    var pattern = this.loginDetailsArray.rules.username.pattern;
    }

2 Answers 2

1

When consuming your service, you get 3 responses, 1 for the data, 1 for the error & 1 for when the call is completed.

loadLoginDetails() {
    this.customerService.getLoginDetails().subscribe(
        res => {
            //Code if your response has data.
        },
        error => {
            //Code if your response has error.
        },
        () => {
            //Code when your request is finished.
        }
    );
}

You can either call your functions from the

res => {}

if you want it to run only if you had a successful response, if you want to run it regardless of your response, run it from

() => {}

Regards!

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

Comments

1

This should just work?

loadLoginDetails() {

  this.customerService.getLoginDetails().subscribe(res => {
    this.loginDetailsArray = res.json();
    this.functions();
  });
}

1 Comment

yes. but if i pass that loadLoginDetails() function in ngOnInit, and try to get the data,it is showing undefined or it is empty. because as it is asynchronous before getting the data ir is calling functions()

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.