Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid 'this' to a Java object in Nativescript Plugin

I am trying to develop a nativescript plugin to perform some functionality with the Azure SDK. The docs for the SDK show below:

enter image description here

So I have added the following function to my plugin:

MobileServiceUser.newUser = function (userId, client) {
    var userObject = com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId); // causing the error
    client.setCurrentUser(userObject);
};

UserId is a string.

However, the top line throws the error:

JS: Error: Trying to link invalid 'this' to a Java object

I have a full repo showing the minimal implimentation to create this issue on Github.

I would be really grateful of any suggestions how to fix this.

like image 589
George Edwards Avatar asked Jul 12 '16 17:07

George Edwards


1 Answers

This answer is a little late but I recently ran into this issue myself. Hopefully this answer helps someone in the future!

The error is a bit plain but if you take a look at the code source, you'll see that it's actually telling you that you cannot link a variable to the Object/Class itself as a reference:

MobileServiceUser.newUser = function (userId, client) {
  var userObject = com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId);
};

When you want to instantiate a class, you need to use the keyword new to signify this action. Without new listed before the class constructor call, the program just sees that you're making a direct link between userObject and the class itself. This should fix your problem:

var userObject = new com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId);

Cheers ~Px

like image 119
Pixxl Avatar answered Nov 10 '22 20:11

Pixxl