Phonegap contact.save duplicate phoneNumbers when updating a contact

I have a problem with Phonegap contacts.save. When I create a contact, it works fine on iOS and Android, but when I try to update a contact, there are duplicate files such as phoneNumbers, emails, URLs, ims, addresses, I use Phonegap 2.1 and Xcode 4.5.1.

Can someone help me solve this problem ?. I appreciate your time. Thanks.

In summary:

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

My code is:

var options = new ContactFindOptions(); options.filter = 20; //just it 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); 
+4
source share
2 answers

The code works exactly as it should in accordance with the W3C Contact specification. This does not mean that it makes sense, though :)

In any case, when you create a new array of phone numbers and set it to the contact.phoneNumbers property, you add the phone numbers to the contact. If you want to edit / replace existing phone numbers, you need to request phone numbers as part of your β€œfields”. Then you need to scroll through the existing phone numbers and edit them as you wish.

Yup, this is contrary to intuition, but for you W3C api.

+1
source

You need to assign a value for the already created contact field for phone numbers instead of a new value for phone numbers using the new ContactField .

So here is an example of updating phone numbers,

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

then instead of using contact.phoneNumbers = tPhoneNumbers; when updating, you immediately call the save method and it.

+1
source

Source: https://habr.com/ru/post/1442516/


All Articles