• Home

ContactFindOptions is not defined (and probably some others too)

Everyone starting to develop on mobile devices with API level frameworks such as Cordova or Phonegap will receive this error message one day. This one could freeze your learning curve for a while. So here is a few steps to get rid of it :

  • check for plugins installation. For this particular error we need the org.cordova.apache.contacts plugin to be installed via cordova plugin add org.apache.cordova.contacts
  • check for permissions : all Cordova/Phonegap plugins have extensive help pages where permission "issues" are described. I am usually having a look to https://github.com/apache?query=cordova which are more readable than details plugin entries on http://plugins.cordova.io/#/
  • never forget to make api calls when your device is ready. You can prepare calls in functions, but only use these functions when the device calls can be done.
<script type="text/javascript">    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    function onDeviceReady() {
        var options = new ContactFindOptions();
        options.filter = "";    
        options.multiple = true;
        var filter = ["displayName", "addresses"];
        navigator.contacts.find(filter, onSuccess, onError, options);
    }
    function onSuccess(contacts) {
        for (var i=0; i<contacts.length; i++) {
            alert(contacts[i].displayName);
        }
    };
    function onError(contactError) {
        alert('onError!');
    };
 </script>

The execution can be made with a <body onload="onLoad()">

Print Email