5

I am trying to call onPaymentStatus() function inside the jQuery function. But i am unable use 'this' scope

this.braintree.client.create({
  authorization: 'client_token'},
  (err, client) => {
      client.request({
        endpoint: 'payment_methods/credit_cards',
        method: 'post',
        data: data
      }, function (requestErr, response) {

       if (requestErr) { throw new Error(requestErr); }
       console.log('Got nonce:', response.creditCards[0].nonce);
       this.onPaymentStatus(response.creditCards[0].nonce); //
    });
});

onPaymentStatus (nonce) {
  console.log(nonce);
}

I got error ERROR TypeError: Cannot read property 'onPaymentStatus' of null

1
  • Angular5 is written in typescript. Anyways after compilation it gets converted into Javascript by the WebPack. So technically you can call it on while on runtime. But my question will be WHY? why can't you write the entire thing in angualr? Do some research Commented Mar 14, 2018 at 19:17

1 Answer 1

14

You need to make a reference to the component outside the jQuery function as the meaning of this changes within it

const component = this;

Then call

component.onPaymentStatus()
Sign up to request clarification or add additional context in comments.

1 Comment

really helpful.

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.