2017-02-01 12 views
0

以下のコードは、コードアンドロイドのsmsアクセス許可のプラグインコードです。それは正常に動作しています。私はそれは私が私が間違っているのか知ってはいけない 未知の型エラー:nullのプロパティ 'getSMS'を読み取ることができません

if the user tap allow then i am trying to call my this.getSMS() as you can see in the above code. if user deny then i am just getting error callback.

エラー

setTimeout(() => { 
     this.holders.dissmissLoadingCustom(); 
     platform.ready().then(() => { 

     var permissions = cordova.plugins.permissions; 

     permissions.hasPermission(permissions.READ_SMS, checkPermissionCallback, null); 

     function checkPermissionCallback(status) { 
      if(!status.hasPermission) { 
      var errorCallback =() => { 
       console.log("invoking the errorCallback"); 
       alert('READ_SMS permission is not turned on'); 
      } 

      permissions.requestPermission(
       permissions.READ_SMS, 
       (status) => { 
       console.log("invoking status"); 

       if(!status.hasPermission) { 
       console.log("invoke !status.hasPermission"); 
       errorCallback(); 
       } 
       else{ 
       console.log("invoking else part !status.hasPermission"); 
       this.getSMS();//this line through error 
       } 
      }, 
      errorCallback); 
      } 
     } 

     }); 

    }, 10000); 
をthroughingされ、私の this.getSMS()関数を呼び出すしようとしています

内部。

+1

これはあなたを助けるかもしれません。 http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work – Prajwal

+0

'checkPermissionCallback'を矢印関数にします –

+0

@Prajwal私は以下のように答えようとしていますが、私はそれをスローすることはできませんあなたが何か考えがある場合は私に知らせてください。 –

答えて

0

通常の機能としてcheckPermissionCallbackを定義しました。 キーワードは、その関数を指し、定義されている実際のクラスを指しません。

setTimeout(() => { 
     this.holders.dissmissLoadingCustom(); 
     platform.ready().then(() => { 

     var permissions = cordova.plugins.permissions; 

     permissions.hasPermission(permissions.READ_SMS, (status)=>checkPermissionCallback(status), null);//use arrow to call 

     function checkPermissionCallback(status) { 
      if(!status.hasPermission) { 
      var errorCallback =() => { 
       console.log("invoking the errorCallback"); 
       alert('READ_SMS permission is not turned on'); 
      } 

      permissions.requestPermission(
       permissions.READ_SMS, 
       (status) => { 
       console.log("invoking status"); 

       if(!status.hasPermission) { 
       console.log("invoke !status.hasPermission"); 
       errorCallback(); 
       } 
       else{ 
       console.log("invoking else part !status.hasPermission"); 
       this.getSMS();//this line through error 
       } 
      }, 
      errorCallback); 
      } 
     } 

     }); 

    }, 10000); 

それとも、関数定義の前に別の変数にthisを保存したり、bindを使用することができます。

+0

どのようなエラーが表示されますか? –

+0

更新されたスニペットを試すことができますか? –

+0

私はsetTimeoutそのものを呼び出すのではありません。 –

関連する問題