Search code examples
javascriptfirefoxmozilla

Firefox OS Contacts API saving contact as unnamed


I'm having a problem with the Firefox OS Contacts API.

I'm just copying the code from this page https://wiki.mozilla.org/ContactsAPI#Create_contact_example

var contact = new mozContact({name: ["John Doe"], givenName: ["John"], familyName: ["Doe"]});

var request = navigator.mozContacts.save(contact);

request.onsuccess = function() {
   alert("Success saving contact. New contact ID: " + contact.id);
};

request.onerror = function() {
   alert("Error saving contact.");
};

But the contact is saved as "No name". I didn't change anything in the code (it's from the official documentation) and the permissions are ok, as readwrite. I'm running on the simulator. Does anyone know why is this happening?


Solution

  • Doh! The example code in that page matches the code on mozilla-central, or the latest version of Gecko/Firefox OS. Things are slightly different in 1.1, you have to initialize the mozContact object like this:

    var contact = new mozContact();
    contact.init({name: ["John Doe"], givenName: ["John"], familyName: ["Doe"]});
    

    Sorry about that. Our Wiki is not the most friendly place to look for example code, since it's targeted at Firefox OS/Gecko hackers, not web developers. We try to keep MDN updated about compatibility issues like this; see the example code in the MDN Contacts page: https://developer.mozilla.org/en-US/docs/WebAPI/Contacts