ScreenOrientation plugin gives runtime error

I’m using ScreenOrientation native plugin.

In my config.xml I have this line, which works as expected:

<preference name="Orientation" value="portrait"/>

In a certain page, I would like to rotate the screen orientation to landscape. This is my code:

import { Component } from '@angular/core';

import { Platform } from 'ionic-angular';
import { ScreenOrientation } from 'ionic-native';


@Component({
  selector: 'my-page',
  templateUrl: 'mypage.html'
})

export class MyPage {

    constructor(public platform: Platform)
    {
        platform.ready().then((readySource) => { 
            //setTimeout(() => {
                ScreenOrientation.lockOrientation('landscape');
            //}, 1000);
        });
    }    
}

Even with the timeout code, I always get this runtime error:

runtime error uncaught (in promise): typeerror: undefined is not an object

My system information:

Cordova CLI: 6.5.0 
Ionic Framework Version: 2.0.0
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.0.0
ios-deploy version: 1.9.1 
ios-sim version: 5.0.13 
OS: macOS Sierra
Node Version: v4.4.0
Xcode version: Xcode 8.2.1 Build version 8C1002

Plugin version:

cordova-plugin-screen-orientation 1.4.2 "Screen Orientation"

I’ve looked around for solutions, and have tried several things including reinstalling ionic and cordova, removing and recreating the platform folders, but I’m stuck on this now.

1 Like

+1
On an Android device this exception is thrown. The property actually sets, or unsets the orientation, but I don’t like getting exceptions.

My code is simply…

  ionViewDidLoad() {

    console.log('Hello Home Page');
    ScreenOrientation.lockOrientation('portrait')



EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
TypeError: Cannot read property 'then' of undefined
    at file:///android_asset/www/build/main.js:423:21
    at file:///android_asset/www/build/main.js:376:17
    at new t (file:///android_asset/www/build/polyfills.js:3:11969)
    at tryNativePromise (file:///android_asset/www/build/main.js:375:20)
    at getPromise (file:///android_asset/www/build/main.js:397:16)
    at wrapOtherPromise (file:///android_asset/www/build/main.js:418:12)
    at Function.<anonymous> (file:///android_asset/www/build/main.js:567:20)
    at Function.value [as lockOrientation] (file:///android_asset/www/build/main.js:631:53)
    at HomePage.ionViewDidLoad (file:///android_asset/www/build/main.js:31740:81)
    at ViewController._lifecycle (file:///android_asset/www/build/main.js:5883:37)
    at ViewController._didLoad (file:///android_asset/www/build/main.js:5760:14)
    at NavControllerBase._didLoad (file:///android_asset/www/build/main.js:47495:14)
    at t.invoke (file:///android_asset/www/build/polyfills.js:3:9655)
    at Object.onInvoke (file:///android_asset/www/build/main.js:39837:37)
    at t.invoke (file:///android_asset/www/build/polyfills.js:3:9606)

I solved this in this thread: