ERROR TypeError: Cannot read property 'then' of undefined while saving contact in IONIC 4

ERROR TypeError: Cannot read property 'then' of undefined while saving contact in IONIC 4

我想将联系人保存到我的 phone 簿中。
我按照 https://ionicframework.com/docs/native/contacts

中的说明进行操作

detail.html

<div class="call">
     <ion-buttons slot="primary">
         <ion-button size="small" (click)="save(memberData.contact_no,memberData.fname,memberData.lname)">
              Save Contact
              <ion-icon slot="end" name="phone-portrait"></ion-icon>
         </ion-button>
     </ion-buttons>
</div>

detail.ts

import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts/ngx';

constructor(private contacts: Contacts) { }

save(number: string, firstName: string, lastName: string) {
    // alert(number + " " + firstName + " " + lastName);

    let contact: Contact = this.contacts.create();

    contact.name = new ContactName(null, firstName, lastName);
    contact.phoneNumbers = [new ContactField('mobile', number)];
    contact.save()
    .then(() => console.log('Contact saved!', contact),
      (error: any) => console.error('Error saving contact.', error))
    .catch(err => console.log('Catch : Error saving contact', err));
  } 

错误

common.js:290 Native: tried calling Contacts.create, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator

MemberDetailPage.html:17 ERROR TypeError: Cannot read property 'then' of undefined

您在浏览器中测试时遇到此错误。尝试为 Android 或在 Xcode 中为 iOS 启动应用的 android 工作室版本,看看插件是否有效。

如果您尝试单独在浏览器中测试 cordova 插件,这可能会有所帮助 https://docs.microsoft.com/en-us/visualstudio/cross-platform/tools-for-cordova/first-steps/simulate-in-browser?view=toolsforcordova-2017

通常始终使用真实设备或模拟器来测试本机功能。