Twomz
Twomz

Reputation: 712

Saving phone number and email to contacts

function saveContact(contactObj) {
  var contact = navigator.contacts.create();
  var email = [1];
  //var len = contactObj.phone.length;
  var len = 2;
  var phoneNums = [len];
  email[0] = new ContactField("email",contactObj.email);
  contact.email = email;
  contact.nickname = contactObj.name;
  contact.givenName = contactObj.name;
  for (var i = 0; i < len; i++) {
    phoneNums[i] = new ContactField(contactObj.phone[i].type, contactObj.phone[i].number, contactObj.phone[i].pref);
  }
  contact.phoneNumbers = phoneNums;

  contact.save(onSaveSuccess, onSaveFail);
}

contactObj is an object that contains the name, email and a list of phone numbers. The code hits onSaveSuccess and adds a contact with the correct name, but no phone numbers or emails are added. Sample input (Stringified for transfer then parsed before being sent to the function)...

{"name":"Test User",
"email":"[email protected]",
"phone":{
  "0":{"type":"cell","number":"1231231234","pref":true},
  "1":{"type":"home","number":"1231231235","pref":false}}}

Anyone know why it isn't adding phonenumbers/emails?

EDIT: Turns out objects don't have a length... only arrays. Email still isn't working though.

Upvotes: 0

Views: 271

Answers (1)

Titouan de Bailleul
Titouan de Bailleul

Reputation: 12949

I think the contact.email should actually be contact.emails and it also should be an array like contact.phoneNumbers.

Here's the exact structure :

{"type":"home","value":"[email protected]","id":0,"pref":false}

Upvotes: 1

Related Questions