0

I am getting the error Uncaught TypeError: Object #<Object> has no method 'getInvoices' when I call this.getInvoicesin the ajax.error result. How can I access the typescript function from there?

// Typescript
class InvoicesController {
     ...

     public getInvoices(skip: number, take: number): void {
         ...    
     }

     public createInvoice() {
          $.ajax({
            ...
            contentType: 'application/json',
            type: 'POST',
            success: function (res) {
                if (res.result === 'ok') {
                    this.getInvoices(0,100); // THIS DOES NOT WORK? 
                }
            },
            error: function (err) {
                this.getInvoices(0,100); // THIS DOES NOT WORK?
            }
        });
     }
}
3
  • 1
    possible duplicate of $(this) inside of AJAX success not working Commented Sep 25, 2013 at 15:39
  • no this has to do with typescript Commented Sep 25, 2013 at 15:40
  • 1
    No, it doesn't. TypeScript compiles to JavaScript. Pass the context argument to $.ajax(). Commented Sep 25, 2013 at 15:41

2 Answers 2

5

check your scope. I believe when you are calling this you're actually referring to the ajax object and not the class InvoicesController

public createInvoice() {
      me = this;
      $.ajax({
         ....
        contentType: 'application/json',
        type: 'POST',
        success: function (res) {
            if (res.result === 'ok') {
                console.log('Data saved1');

            }
            else {
                console.log('Save error1');
            }
        },
        error: function (err) {
            me.getInvoices(100,0); // TRY THIS

            console.log("error2"+err);
        }
    });
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Use short typescript functions syntax, it captures class context by default:

// Typescript
class InvoicesController {
 ...

 public getInvoices(skip: number, take: number): void {
     ...    
 }

 public createInvoice() {
      $.ajax({
        ...
        contentType: 'application/json',
        type: 'POST',
        success: (res) => {
            if (res.result === 'ok') {
                this.getInvoices(0,100); // WORK NOW 
            }
        },
        error: (err) => {
            this.getInvoices(0,100); // WORK NOW
        }
    });
 }

}

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.