如何更新sms读取插件的变量

时间:2016-11-16 07:44:22

标签: javascript angular sms ionic2

我正在尝试在我的短信读取插件代码的成功回调中进行一次调用。 在这里,我可以在控制台中打印我的_this.otpnumber

请查看我的堆栈跟踪img

getSMS(){
          var _this= this;    

          var filter = {
                box : 'inbox', // 'inbox' (default), 'sent', 'draft', 'outbox', 'failed', 'queued', and '' for all

                // following 4 filters should NOT be used together, they are OR relationship
                // read : 0, // 0 for unread SMS, 1 for SMS already read
                // _id : 1234, // specify the msg id
                address : 'IM-WAYSMS', // sender's phone number
                // body : 'This is a test SMS', // content to match

                // following 2 filters can be used to list page up/down
                indexFrom : 0, // start from index 0
                maxCount : 10, // count of SMS to return each time
              };

              if(window.SMS) window.SMS.listSMS(filter, function(data){
                  //Filterd Data
                  _this.logger.debug("checking the filter data " +JSON.stringify(data));
                  _this.text=data[0].body;
                 console.log("checking the text " + _this.text);
                  _this.otpNumber = _this.text.substring(Number(_this.text.indexOf("'"))+Number(1), Number(_this.text.lastIndexOf("'")))

                  console.log("checking the otpNumber "+_this.otpNumber);

                  if(_this.otpNumber){
                    this.user.validateOTP(_this.otpNumber,_this.loginId, (result,data)=>{
                        if(result == '1'){
                          alert("otp success");
                        }
                        else{
                          console.log("error of otp"+data);
                        }
                    });
                  }
                  else{
                    console.log("no otpnumber");
                  }
                 // return _this.otpNumber;   
                }, function(err){
                  console.log("checking the err "+JSON.stringify(err));
              });

                console.log("checking the otpNumber after if "+_this.otpNumber);
               console.log("printin the text");


        }
  

在我的_this.otpNumber打印后,我收到了EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'user' of null

这样的错误

1 个答案:

答案 0 :(得分:0)

更改

function(data){

(data => {

this.继续指向当前的组件实例。

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions