Uncaught ReferenceError: ContactFindOptions is not defined

怎甘沉沦 提交于 2019-12-13 05:27:48

问题


I have a problem with cordova/phonegap contacts.
This is the code i am trying to execute, i put in an external javascript file:

function onDeviceReady() {
// find all contacts
var options = new ContactFindOptions();
options.filter = "*";
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);
}

// onSuccess: Get a snapshot of the current contacts

function onSuccess(contacts) {
for (var i = 0; i < contacts.length; i++) {
    console.log("Display Name = " + contacts[i].displayName);
}
}

// onError: Failed to get the contacts

function onError(contactError) {
alert('onError!');
}

This is the code from the phonegap API documentation: link

The original code is to find all contacts with 'bob' in every field.
I changed it to "*"(Just a star) for all my contacts.

The function onDeviceReady is just called by a button click.

The error I get in the logcat is this:

[INFO:CONSOLE(81)] "Uncaught ReferenceError: ContactFindOptions is not defined"  
81 is the linenumber with: var options = new ContactFindOptions();

Does anyone know what to do to get the function ContactFindOptions() work?

If you need more info, just let me know.


回答1:


Access contacts on Android using Phonegap:-

function onDeviceReady() {
    // specify contact search criteria
    var options = new ContactFindOptions();
    options.filter="";          // empty search string returns all contacts
    options.multiple=true;      // return multiple results
    filter = ["displayName"];   // return contact.displayName field

    // find contacts
    navigator.contacts.find(filter, onSuccess, onError, options);
}

var names = [];

// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
    for (var i=0; i<contacts.length; i++) {
        if (contacts[i].displayName) {  // many contacts don't have displayName
            names.push(contacts[i].displayName);
        }
    }
    alert('contacts loaded');
}

you can try to read the documentation and implementation of how to use I'm providing some link which will help you Saving Contacts with PhoneGap Android and Example of Creating a full contact with PhoneGap




回答2:


This may be happening because your cordova is not understanding this function. You need to add the plugin, you need to add the contacts plugin. In your cordova project run this using command line:

cordova plugin add org.apache.cordova.contacts

This will add following to AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

and add following to res\xml\config.xml

<feature name="Contacts">
   <param name="android-package" value="org.apache.cordova.contacts.ContactManager" />
</feature>



回答3:


I experienced this same issue because the deviceready event in Cordova had yet to fire. Addresss this by wrapping any Cordova API calls in a deviceready event listener:

document.addEventListener("deviceready", function(){
    console.log("deviceready fired!"); //do your stuff
});



回答4:


Had the same issue, added this line of code and it worked. it links to the cordova library.

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript">
    app.initialize();
</script>


来源:https://stackoverflow.com/questions/20262094/uncaught-referenceerror-contactfindoptions-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!