Search code examples
javascriptandroidcordovacordova-3

Uncaught ReferenceError: ContactFindOptions is not defined


I have a problem with cordova/phonegap contacts.
This is the code i am trying to execute, i put in an external javascript file:

function onDeviceReady() {
// find all contacts
var options = new ContactFindOptions();
options.filter = "*";
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);
}

// onSuccess: Get a snapshot of the current contacts

function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
    console.log("Display Name = " + contacts[i].displayName);
}
}

// onError: Failed to get the contacts

function onError(contactError) {
alert('onError!');
}

This is the code from the phonegap API documentation: link

The original code is to find all contacts with 'bob' in every field.
I changed it to "*"(Just a star) for all my contacts.

The function onDeviceReady is just called by a button click.

The error I get in the logcat is this:

[INFO:CONSOLE(81)] "Uncaught ReferenceError: ContactFindOptions is not defined"  
81 is the linenumber with: var options = new ContactFindOptions();

Does anyone know what to do to get the function ContactFindOptions() work?

If you need more info, just let me know.


Solution

  • Access contacts on Android using Phonegap:-

    function onDeviceReady() {
        // specify contact search criteria
        var options = new ContactFindOptions();
        options.filter="";          // empty search string returns all contacts
        options.multiple=true;      // return multiple results
        filter = ["displayName"];   // return contact.displayName field
    
        // find contacts
        navigator.contacts.find(filter, onSuccess, onError, options);
    }
    
    var names = [];
    
    // onSuccess: Get a snapshot of the current contacts
    //
    function onSuccess(contacts) {
        for (var i=0; i<contacts.length; i++) {
            if (contacts[i].displayName) {  // many contacts don't have displayName
                names.push(contacts[i].displayName);
            }
        }
        alert('contacts loaded');
    }
    

    you can try to read the documentation and implementation of how to use I'm providing some link which will help you Saving Contacts with PhoneGap Android and Example of Creating a full contact with PhoneGap