离子 4 中的错误是 "Cannot read property 'contact' of null"

An error is "Cannot read property 'contact' of null" in ionic 4

我正在 ionic 4 中创建来电者姓名扬声器应用程序。为此,我能够获取来电者状态和号码。有了号码,我要找联系人里的名字

此处代码:

phonecalls() {
PhoneCallTrap.onCall(function (obj) {
  alert("CHANGE STATE: " + obj.state);
  var callObj = JSON.parse(obj),
    state = callObj.state,
    callingNumber = callObj.incomingNumber;
  console.log("obj: " + obj);

  //working
  alert("callingNumber STATE: " + callingNumber);

  //call name get
  const options = new ContactFindOptions();
  options.filter = callingNumber;
  options.multiple = true;
  options.hasPhoneNumber = true;
  this.contact.find(['*'], options).then((contacts) => {
    this.contactsfoundcallingNumber = contacts[0].displayName;
    console.log(JSON.stringify(contacts[0]));
  });

  alert("well well: " + this.contactsfoundcallingNumber)

  switch (state) {
    case "RINGING":
      console.log("Phone is ringing", callingNumber);
      break;
    case "OFFHOOK":
      console.log("Phone is off-hook");
      break;
    case "IDLE":
      console.log("Phone is idle");
      break;
  }
});
}

此处chrome使用 USB 调试检查

将回调函数更改为箭头函数,this 引用将被保留。

PhoneCallTrap.onCall((obj) => {
    // rest of the code
})