Ali B
Ali B

Reputation: 1117

Nativescript app fails to build after including platform.js

I have a Nativscript 5.0 app that needs access to platform info (device, screen). I added the tns core module platform.js and then I rebuilt the whole project with 'tns run android'.

Now I'm getting this error:

System.err: TypeError: Cannot read property 'getContentResolver' of undefined
tns_modules/tns-core-modules/platform/platform.js', line: 79, column: 83

This error refers to this code in platform.android.js:

Object.defineProperty(Device.prototype, "uuid", {
        get: function () {
            if (!this._uuid) {
                var nativeApp = appModule.android.nativeApp;
                this._uuid = android.provider.Settings.Secure.getString(nativeApp.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
            }
            return this._uuid;
        },
        enumerable: true,
        configurable: true
    });

Specifically, nativeApp seems to be undefined. I'm running this code thru an Android simulator. What am I missing here? do I need to define the OS explicitly somewhere to prevent this error?

Thanks

Upvotes: 1

Views: 287

Answers (2)

Sebj
Sebj

Reputation: 496

I had this error when trying to get device.uuid. I managed to get it work by using launchEvent:

import { on as applicationOn, launchEvent } from 'tns-core-modules/application'
import { device } from 'tns-core-modules/platform'

applicationOn(launchEvent, () => {
    // Use device.uuid here
})

Upvotes: 1

Manoj
Manoj

Reputation: 21908

You must be accessing the platform module too early, even before the nativeApp instance is fully initialised and ready to be used.

You may wait until the displayed / activityCreated event Or simply the loaded event of any component, which ensures that the nativeApp will be ready.

Upvotes: 2

Related Questions