Seho Lee
Seho Lee

Reputation: 4108

phonegap) error occurs when retriving iphone contact emails

I am using contact.find function to get emails from contact list.
getting all contactlist names is fine but I am stuck from getting contactlist emails.
it seems javascript error occurs when I parse email data from contactlist.
below is my code.

var contactFxn = {

getContact : function(){
    var options = new ContactFindOptions();
    var fields = ["name", "email"];
    options.filter= "";
    options.multiple=true;
    navigator.contacts.find(fields, this.onSuccess, this.onError, options);
    //console.log(options);
},
onSuccess : function(contacts){

    for(var i = 0; i < contacts.length; i++){
            console.log(i);
            console.log("Display email = " + contacts[i].emails[0].value);
        }  
    }
},
onError : function(e){
    console.log(e);
}

}

contactFxn.getContact();

and here is my log console.

2012-11-09 16:48:22.564 test[9529:907] [LOG] 0
2012-11-09 16:48:22.566 test[9529:907] [LOG] Display Name = ssxxc
2012-11-09 16:48:22.568 test[9529:907] [LOG] 1

I am guesssing webview throws javascript error when contact does not have any email value.
It is too bad that I can't check what type of javascript error occured in xcode.

I already tried 'if(contacts[i].emails[0] == null)' but no luck. webview throws error anyway
can anybody suggeust other ways to fix this problem? thanks in advance.

Upvotes: 0

Views: 646

Answers (1)

Seho Lee
Seho Lee

Reputation: 4108

onSuccess : function(contacts){

for(var i = 0; i < contacts.length; i++){
        console.log(i);
        if(contacts[i].emails){
            console.log("Display email = " + contacts[i].emails[0].value);
        }
    }  
}

just stupid mistake.

I tried to get contacts[i].emails[0].value while some elements of contacts array

does not have emails value. above code works fine now

Upvotes: 2

Related Questions