Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

javascript - Uncaught ReferenceError: ContactFindOptions is not defined

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 resxmlconfig.xml

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...