Question

I'm working with phonegap, My actual code shows the names of each contact in my mobile phone but when I want to show the phone numbers shows me a "null". Some contacts don't have phone numbers, google contacts for example but I've tested only with some contacts which have phone number saved and doesen't work either !

The error exactyle is : "TypeError: Cannot read property '0' of null". I understand the error but I don't understand why show me this if I tested one contact which have phone number.

I'm following the official tutorial from http://docs.phonegap.com/en/1.4.1/phonegap_contacts_contacts.md.html#ContactField

EDIT CODE ADDED

function onDeviceReady() {
    // specify contact search criteria
    var options = new ContactFindOptions();
    options.filter = "pepepe";      // 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);
}

Could be the problem in filter var ?

function onSuccess(contacts) {


for (var i=0; i<contacts.length; i++) {
    document.write(contacts[i].displayName);
    tlf=contacts[i].phoneNumbers;
    try{
    document.write(contacts[i].phoneNumbers[0].value);
    }
    catch(err){
    window.alert(err);
    }
    if (tlf== null){
    document.write("No hi han telefons");
    }else{
    document.write(contacts[i].phoneNumbers[0].value);
    }
    /*
    for (var j=0; j<contacts[i].phoneNumbers.length; j++) {
        document.write("Type: " + contacts[i].phoneNumbers[j].type + "\n" + 
                "Value: "  + contacts[i].phoneNumbers[j].value + "\n" + 
                "Preferred: "  + contacts[i].phoneNumbers[j].pref);
    }
    */
    document.write("<br/>");
}

};

EDIT: It's very strange that always return null, if there are phone numners registered !

Was it helpful?

Solution

It looks like you're using outdated documentation. From the comments you're using version 3.x. Here's the documentation on how to use it now.

It looks like what's different now is that you have to specify which fields you want to be able to use them.

function onSuccess(contacts) {
    alert('Found ' + contacts.length + ' contacts.');
};

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

// find all contacts with 'Bob' in any name field
var options      = new ContactFindOptions();
options.filter   = "Bob";
options.multiple = true;
var fields       = ["displayName", "name"]; //here you would add phoneNumbers
navigator.contacts.find(fields, onSuccess, onError, options);

Or if you want to get all the fields the Documentation says:

A contactFields value of "*" returns all contact fields.

OTHER TIPS

hey just check for the object whether it is null or not before proceeding. This helped me get around that error.

Here is the check

if(contacts[i].phoneNumbers != null) {

..................... ......................

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top