Ionic Native Contact promise not working

import { Component, OnInit} from '@angular/core';
import {Contacts,Contact,IContactFindOptions,ContactFieldType,} from '@ionic-native/contacts/ngx';

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page implements OnInit {
  
  contact:Contact
  field:ContactFieldType[] = ["displayName", "phoneNumbers"];
  constructor(private contacts:Contacts
              ){
  }
  test:any = [];
  ngOnInit(){
    
  }
  getContacts(){
     let options:IContactFindOptions = {
       filter:'r',

     }
    this.contacts.find(this.field,options)
    .then((contacts) => {
      console.log(contacts);
      for(let i=0;i<contacts.length;i++){
        let contact = {
          name:'',
          phone:''
        }
        contact.name = contacts[i].displayName;
        contact.phone = contacts[i].phoneNumbers[0].value;
        this.test.push(contact);
      }
      
    });

  }
}


Error:
Tab1Page.html:13 ERROR TypeError: Cannot read property ‘then’ of undefined
at Tab1Page.push…/src/app/tab1/tab1.page.ts.Tab1Page.getContacts (tab1.page.ts:25)
at Object.eval [as handleEvent] (Tab1Page.html:13)
at handleEvent (core.js:23107)
at callWithDebugContext (core.js:24177)
at Object.debugHandleEvent [as handleEvent] (core.js:23904)
at dispatchEvent (core.js:20556)
at core.js:21003
at HTMLElement. (platform-browser.js:993)
at ZoneDelegate.push…/node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:17290)

Your code is kinda messy… please clean up before posting it, like the unnecessary comma after ContactFieldType in your import, unnecessary breakspaces, etc.

I have never used the Cordova Contacts plugin, but it appears there are two required parameters for contacts.find, your options is probably referring to one of the optional parameters. But the command actually takes options in as its second mandatory parameter.

In short you just used it wrongly.

Parameters

  • contactFields : Contact fields to use as a search qualifier. (DOMString[]) [Required]
  • contactSuccess : Success callback function invoked with the array of Contact objects returned from the database. [Required]
  • contactError : Error callback function, invoked when an error occurs. [Optional]
  • contactFindOptions : Search options to filter navigator.contacts. [Optional]Keys include:
    • filter : The search string used to find navigator.contacts. (DOMString) (Default: "" )
    • multiple : Determines if the find operation returns multiple navigator.contacts. (Boolean) (Default: false )
    • desiredFields : Contact fields to be returned back. If specified, the resulting Contact object only features values for these fields. (DOMString[]) [Optional]
    • hasPhoneNumber (Android only): Filters the search to only return contacts with a phone number informed. (Boolean) (Default: false )

Source: https://github.com/apache/cordova-plugin-contacts