Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap contact.save duplicate phoneNumbers when update a contact

I have an issue with Phonegap contacts.save. When I create a contact works perfectly in iOS and Android, but, when I try to update a contact, there are duplicates fileds as phoneNumbers, emails, urls, ims, addresses, I'm using Phonegap 2.1 and Xcode 4.5.1.

Someone could help me to solve this issue?. I appreciate your time. Thanks.

In resume:

  • navigator.contacts.create(); works correctly.
  • when I try to update a contact, save correctly, but, add phone numbers instead update.

My code is:

var options = new ContactFindOptions();
options.filter = 20;  //just it's an example. Looking for id 20.
var fields = ['id'];
var contact;   
navigator.contacts.find(fields,function(contacts){
    if (contacts.length==0) 
       contact = navigator.contacts.create();
    else
       contact = contacts[0];

    var tContactName = new ContactName();
    tContactName.givenName = 'Name';
    tContactName.LastName = 'LastName';
    contact.name = tContactName;    

    var tPhoneNumbers[2];
    tPhoneNumbers[0] = new ContactField('work', '123456789012',true);
    tPhoneNumbers[1] = new ContactField('home', '120987654321', false);
    contact.phoneNumbers = tPhoneNumbers;

    contact.save(function(contact) {
       navigator.notification.alert('Saved sucessfully!!!',function(){},'Title');
    }, function(contactError) {
       navigator.notification.alert('Error contact save: '+contactError.code,function(){},'Title');
    }
}, function(contactError) {
       navigator.notification.alert('Error contact find: '+contactError.code,function(){},'Title');
}, options);
like image 644
user1779665 Avatar asked Nov 03 '22 12:11

user1779665


2 Answers

The code is working exactly how is should according to the W3C Contact spec. That doesn't mean it makes sense though :)

Anyway, when you create a new array of phone numbers and set that to be equal to the contact.phoneNumbers property in effect you are adding phone numbers to the contact. If you want to edit/replace the existing phone numbers you need to request the phone numbers as part of your "fields". Then you need to loop through the existing phone numbers and edit them as you see fit.

Yup, it is counter-intuitive but that's the W3C api for you.

like image 102
Simon MacDonald Avatar answered Nov 12 '22 17:11

Simon MacDonald


You have to assign value to already created phone numbers contact field instead of creating new value again for the phone numbers using new ContactField.

So here is the example to update phone numbers,

contact.tphoneNumbers[0].value = 123456789012 
contact.tphoneNumbers[1].value = 120987654321

then instead of using contact.phoneNumbers = tPhoneNumbers; on update you directly call the save method and that's it.

like image 30
Naresh Ramoliya Avatar answered Nov 12 '22 18:11

Naresh Ramoliya