1
votes

I am using ionic 2

Downloaded the plugin from https://github.com/salbahra/cordova-plugin-networkinterface

Can't use any global variable or call any other function inside the function

networkinterface.getIPAddress(function (ip) { alert(ip);});

If I use something like this

networkinterface.getIPAddress(function (ip) 
{ alert(ip);
this.test(ip); });

test(x){
console.log("IP = "+ x);}

I get an error:

Error in Success callbackId: networkinterface1280836273 : TypeError: Cannot read property 'test' of null.

I get the Ip alert properly but I can't access it outside the provided function. Also typerscript gives the error:

[ts] Cannot find name 'networkinterface'.

When I use the plugin. But it still compiles and works.

Any idea how to solve the issue?

1
I guess the this-pointer is not what you think it is... Take a look at this answer. I think your doing the same mistake as I did. - Phonolog
The errors seem to be saying that the plugin is not fully present, even though it still works (strange). I've had similar issues where the cause was the plugin not having been added. I would try adding the plugin manually to the project to be sure (not sure how to do this using ionic). - Chris Neve
thanks @Phonolog for the help - Ashutosh
@ThomCunningham no the plugin is correctly installed. Apparently I need to do something with the typings file. Need to add some reference to it. Still trying to figure out how to remove [ts]connot find name 'networkinterface'. although the plugins works fine - Ashutosh

1 Answers

4
votes

Solved..(thanks to the ionic community) need to use to the lambda function => to capture the meaning of this from the surrounding context. More info here

loadIPAddress() {
    networkinterface.getIPAddress((ip) => {
      alert(ip);
      this.test(ip);
    });
  }

  test(x) {
    console.log("IP = "+ x);
  }