Ionic 2 contact plugin throws error

The plugin into success function of plugin but later throws error as below

Uncaught TypeError: plugin.constructor.getPluginRef is not a function.

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

@Injectable()
export class ContactService {

constructor(private contacts: Contacts) {
	console.log("contacts constructor called " + this.contacts);
}

getAllContacts(){
	this.contacts.find(['phoneNumbers','displayName','givenName','familyName']).then(
		(res : Contact[]) => {
			console.log("contacts fetched " + res);
		},
		(error: any) => {
			console.log('Error gettings contact.', error)
		}
	);
  
}


constructor(platform: Platform, contactService:ContactService) {
    platform.ready().then(() => {
         console.log("Getting contacts from device");
         contactService.getAllContacts();
    });

/**
     * Returns an array of Contacts matching the search criteria.
     * @param fields that should be searched
     * @param successCB success callback
     * @param errorCB error callback
     * @param {ContactFindOptions} options that can be applied to contact searching
     * @return array of Contacts matching search criteria
     */
    find: function(fields, successCB, errorCB, options) {
        argscheck.checkArgs('afFO', 'contacts.find', arguments);
        if (!fields.length) {
            if (errorCB) {
                errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR));
            }
        } else {
            // missing 'options' param means return all contacts
            options = options || { filter: '', multiple: true };
            var win = function(result) {
                var cs = [];
                for (var i = 0, l = result.length; i < l; i++) {
                    cs.push(convertUtils.toCordovaFormat(contacts.create(result[i])));
                }

            **Getting error here**

            successCB(cs);

        };
        exec(win, errorCB, "Contacts", "search", [fields, options]);
    }
},

How would you suggest anybody reading this go about attempting to replicate your problem?

I am getting error at this line as success is executed.

This code snippet is from contacts.js file from plugin dir of contact plugin.

How is the contacts property of the class defining getAllContacts() populated?

I have updated the snippet now. Please check

Judging from #1265 it might be useful to try v3.3.0.

Updated to latest ionic and angular and ionic-native still same error

Error in Success callbackId: Contacts1104342839 : TypeError: plugin.constructor.getPluginRef is not a function
cordova.js:314 Uncaught TypeError: plugin.constructor.getPluginRef is not a function
    at checkAvailability (file:///android_asset/www/build/main.js:59419:40)
    at new Contact (file:///android_asset/www/build/main.js:59131:113)
    at processContact (file:///android_asset/www/build/main.js:59381:22)
    at Array.map (native)
    at file:///android_asset/www/build/main.js:59348:34
    at win (file:///android_asset/www/plugins/cordova-plugin-contacts/www/contacts.js:58:17)
    at Object.callbackFromNative (file:///android_asset/www/cordova.js:293:58)
    at <anonymous>:1:9

snapshot of package.json, updated everything to latest.

  "dependencies": {
"@angular/common": "4.0.0",
"@angular/compiler": "4.0.0",
"@angular/compiler-cli": "4.0.0",
"@angular/core": "4.0.0",
"@angular/forms": "4.0.0",
"@angular/http": "4.0.0",
"@angular/platform-browser": "4.0.0",
"@angular/platform-browser-dynamic": "4.0.0",
"@angular/platform-server": "4.0.0",
"@ionic-native/contacts": "^3.3.0",
"@ionic-native/core": "3.3.0",
"@ionic-native/facebook": "^3.3.0",
"@ionic-native/splash-screen": "3.3.0",
"@ionic-native/status-bar": "3.3.0",
"@ionic/storage": "2.0.0",
"ionic-angular": "2.3.0",
"ionicons": "3.0.0",
"rxjs": "5.2.0",
"sw-toolbox": "3.6.0",
"zone.js": "0.8.5"
  },
  "devDependencies": {
    "@ionic/app-scripts": "1.2.2",
    "typescript": "2.1.5"
  }

Fixed in https://github.com/driftyco/ionic-native/commit/6e2013734094afabb956c1ad424fbe4f05cc48eb

Please update @ionic-native/contacts to 3.3.1.

Thanks for pointing this issue out.

1 Like

Thanks it is working now, using 3.4.0.

Awesome turnaround. Happy :slight_smile: