Calling function inside callback throws error in Angular

binoculars

I use a Cordova plugin which has two callback functions:

window.plugins.screensize.get(this.screensize_success, this.screensize_error);

Next, in the success callback, I do:

screensize_success(result)
{
    console.log(result); // <--- works fine
    console.log("##### height: "+result.height); // <--- works fine
    this.get_highest(result); // <--- throws an error
}
get_highest(result)
{
  return Math.max(result.height,result.width);
}

I get this error:

Uncaught (in promise): TypeError: Cannot read property 'get_highest' of null

What am I doing wrong?

JLRishe

You're losing the this context when the callback is called.

Simplest way to remedy this is to bind the callbacks to this:

window.plugins.screensize.get(this.screensize_success.bind(this), this.screensize_error.bind(this));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling the main function inside a callback function in javascript

From Dev

Angular - calling service function inside eventlistener function returns undefined error

From Dev

Angular2 Component inside ngFor throws Error (viewFactory is not a function)

From Dev

JS - Calling class methods inside of callback function

From Dev

Calling a module function inside a Nodejs callback

From Dev

Calling a function without callback inside a Async module

From Javascript

Javascript callback function throws error "Callback is not a function" in firefox

From Dev

callback function throws unexpected "nonconformant arguments" error

From Dev

JavaScript application bug: calling a function inside another throws a "Maximum call stack size exceeded" error

From Dev

calling function inside angular factory

From Dev

Calling a function inside .then() is throwing an error

From Dev

There is any difference between callback function and calling a function inside another function?

From Dev

Calling any function from dll throws error

From Dev

Typescript throws no error when calling nullable function

From Dev

Calling useRef inside a callback

From Dev

Function throws an error if not run from inside a $timeout

From Dev

Yield inside .then function of promise response throws error

From Dev

Getting error in angular function calling

From Dev

What is the equivalent of not calling a callback when inside an async function?

From Dev

Scope issue calling a callback function inside a loop in node.js

From Dev

Angular calling another function within a click callback function (this)

From Dev

Creating and calling a custom function inside an angular class

From Dev

callback function throws a error in scipy.optimize.minimize class

From Dev

Angular runtime error calling a function inside the render() loop: 'undefined is not an object (evaluating this.watchLocation)'

From Dev

Calling function with callback in nodejs

From Dev

Calling callback function in props

From Dev

Typescript throws no error when calling a function which expects (spread) arguments

From Dev

Calling function throws error / running lines manually does not

From Dev

Nodejs - Re-Calling function on error callback - Is there a non blocking way?

Related Related

  1. 1

    Calling the main function inside a callback function in javascript

  2. 2

    Angular - calling service function inside eventlistener function returns undefined error

  3. 3

    Angular2 Component inside ngFor throws Error (viewFactory is not a function)

  4. 4

    JS - Calling class methods inside of callback function

  5. 5

    Calling a module function inside a Nodejs callback

  6. 6

    Calling a function without callback inside a Async module

  7. 7

    Javascript callback function throws error "Callback is not a function" in firefox

  8. 8

    callback function throws unexpected "nonconformant arguments" error

  9. 9

    JavaScript application bug: calling a function inside another throws a "Maximum call stack size exceeded" error

  10. 10

    calling function inside angular factory

  11. 11

    Calling a function inside .then() is throwing an error

  12. 12

    There is any difference between callback function and calling a function inside another function?

  13. 13

    Calling any function from dll throws error

  14. 14

    Typescript throws no error when calling nullable function

  15. 15

    Calling useRef inside a callback

  16. 16

    Function throws an error if not run from inside a $timeout

  17. 17

    Yield inside .then function of promise response throws error

  18. 18

    Getting error in angular function calling

  19. 19

    What is the equivalent of not calling a callback when inside an async function?

  20. 20

    Scope issue calling a callback function inside a loop in node.js

  21. 21

    Angular calling another function within a click callback function (this)

  22. 22

    Creating and calling a custom function inside an angular class

  23. 23

    callback function throws a error in scipy.optimize.minimize class

  24. 24

    Angular runtime error calling a function inside the render() loop: 'undefined is not an object (evaluating this.watchLocation)'

  25. 25

    Calling function with callback in nodejs

  26. 26

    Calling callback function in props

  27. 27

    Typescript throws no error when calling a function which expects (spread) arguments

  28. 28

    Calling function throws error / running lines manually does not

  29. 29

    Nodejs - Re-Calling function on error callback - Is there a non blocking way?

HotTag

Archive