Issues with the navigator.contacts.find function occurring when using cordova or phonegap

I am attempting to retrieve the contacts stored on a mobile device. The code snippet I am using is not functioning as expected - specifically, the navigator.contacts.find method isn't producing any outcomes. There are no error messages or success confirmations. Additionally, adding an alert message after that particular line of code does not display anything.

function extract_contacts(){

var options = new ContactFindOptions();

options.filter = "";  //if left empty, all contacts will be returned

options.multiple = true;  //multiple results will be returned

var filter = ["displayName"];    //an array used to compare against the options.filter 

navigator.contacts.find(filter, successFunction, errorCallback, options); //issue arises here

function successFunction( matches ){
    alert("Collecting contact information...");
    for( var i=0; i<matches.length; i++){
        alert(matches[i].displayName);
    }
}

function errorCallback(){
    alert("Unable to find contacts.");
}

}

Answer ā„–1

Phonegap Documentation

Check out this example -

function onDeviceReady() {
    var options = new ContactFindOptions();
    options.filter = "";          // searching with empty string to get all contacts
    options.multiple = true;      // return multiple results
    filter = ["displayName", "name"];   // retrieve contact displayName
    navigator.contacts.find(filter, onSuccess, onError, options);
}

// onSuccess: Retrieve current contacts

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

// onError: Failed to retrieve contacts

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

Use this code snippet to find all contacts based on their display names.

Answer ā„–2

CSS

<div class="container"></div>

JQUERY

function display_content(){
   var options = new ContentDisplayOptions();
   options.filter="";
   options.filter="";
   options.multiple=true;
   var elements = ["*"];  
   $.content.find(elements, showSuccess, showError, options);
}

// show all content information
function showSuccess(contents) {
   var data = '';
   $.each(contents, function(index, value) {
        if(value.title){
            $.each(value.title, function(key, value) {
               if(key == 'name'){
                   title = value;
               }                      
            });
        }
        if(value.description){
            $.each(value.descriptions, function(i, val) {
                description = val.value;
            });
        }                    
        data += '<li style="text-decoration:none;">'+title+' '+description+'</li>';
   }); 

   $(".container").html(data);   
}

function showError(contentError) {
   alert('An error occurred!');
}

Answer ā„–3

Do not forget to include desiredFields in your request:

options.desiredFields = [navigator.contacts.fieldType.id, navigator.contacts.fieldType.formatted, navigator.contacts.fieldType.name, navigator.contacts.fieldType.phoneNumbers];

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with HTML and Escaping Challenges in jQuery Functions

Here is a string I have: var items = "<div class='item'><div class='item-img' style='background-image: url('images.123.jpg')'></div></div>" I am looking to update the inner HTML of a div: $ ...

"Can you provide guidance on binding data in vue.js when there is a dash (-) in the key of the JSON

My JSON data is structured as follows: { "color-1": "#8888", "color-2": "#000" } I am attempting to bind this variable with a style tag for a Vue component. However, my current approach seems to not be functioning as expected. <div v-bind:sty ...

Handling extensive base64 encoded data within large XML elements with Simple Framework

I'm currently dealing with memory issues while working on an Android app that involves parsing a large XML file. The main culprit for the file size are the numerous elements containing base64 encoded images. Without these blobs, the XML becomes much m ...

What is the correct method for service injection in Angular 8?

I have encountered an issue while trying to inject a service into my main "App" component. The error message is shown in the screenshot below: constructor(private newsApi: NewsApiService) {} Upon importing the service using the code above, I received the ...

Managing various iterations of identical repositories

Currently in the process of constructing a library of unique react components that are being utilized in various downstream applications. To incorporate these components into my downstream applications, I rely on the package.json. Recently, I began ponde ...

Is there a problem with addEventListener returning false?

What does keeping the third parameter as false in the line below signify? var el = document.getElementById("outside"); el.addEventListener("click", modifyText, false); ...

You must provide a secret or key in order to use the JwtStrategy

I have encountered the following error and I am unsure of its cause. Can you assist me? ERROR [ExceptionHandler] JwtStrategy requires a secret or key TypeError: JwtStrategy requires a secret or key at new JwtStrategy (C:\Users\wapg2\OneDriv ...

What is the best way to implement a Cascading Async Select feature using @atlaskit/select library?

I recently delved into React and I'm currently exploring how to create a Cascading Async Select (for example, selecting Country then City) using @atlaskit/select. As per the documentation, I utilized the option loadOptions in the initial Async Select ...

Tips for controlling HTML elements using JavaScript

I'm currently working on implementing a mouse-over scale effect for an HTML image. I chose to use JavaScript for this task because I need the ability to manipulate multiple elements in different ways simply by hovering over one element. Below is the J ...

Having an issue with ng-model not functioning correctly in md-menu with AngularJS material

I'm currently using md-menu from Angular Material and I have integrated a search box with ng-model in the list. However, I am facing an issue where I cannot access ng-model because 'md-menu-content' is out of scope. Here is my code: <div ...

Creating an Ajax Post request for API invocation

I'm currently setting up an Ajax Post request to interact with an API. Here is a mock curl command for reference: curl -X POST \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --h ...

Having issues when dynamically adding options to a multiselect dropdown

I'm working on dynamically adding options to a multiselect using AJAX: <select size='2' name="CraftCode" id=@ccrf class="form-control js-select manualentrydd" ></select> $.ajax({ type: "GET", url: "GetCraftCodes", data: ...

Implementing pagination and filtering in a single MERN controller

Is it possible to implement pagination and filtering in the same controller? Filter service:- const filterPosts = async (filterData, token) => { const config = { headers: { Authorization: `Bearer ${token}`, }, data: { ...

Using AngularJS ng-repeat with jQuery find yields a single element result

I'm in the process of developing my very first AngularJS application, and I've run into an issue with the jQuery find function. Essentially, what I'm attempting to do is utilize a custom HTML component that contains a list of buttons generat ...

Encountering error #426 in NextJs when the app is loaded for the first time in a new browser, but only in a

Encountering a strange error exclusively in production, particularly when loading the site for the first time on a new browser or after clearing all caches. The website is built using nextjs 13.2.3 and hosted on Vercel. Refer to the screenshot below: http ...

Node.js Mistake - 'Attempting to import outside of a module is not allowed'

Iā€™m currently immersed in a React project, and my focus is... Firebase.js import firebase from 'firebase/app' import 'firebase/database' import 'firebase/auth' const firebaseConfig = { apiKey: process.env.FIREBASE_AP ...

Fuel Calculation - Unable to pinpoint the error

My JavaScript Code for Calculating CO2 Emissions I'm working on a program that calculates how much CO2 a person produces per kilometer. This is part of my school project and I'm still learning, so please bear with me... I also just signed up on ...

Can you explain the significance of argument[0] in JavascriptExecutor?

I recently implemented the "How to handle hidden web elements using the JavaScript executor method". However, I am still unclear about how the method works. public static void selectDateByJS(WebDriver driver, WebElement element, String dateVal) { Javas ...

Using the 'useMediaQuery' function from Material UI in a class component

Currently experimenting with Material UI's useMediaQuery feature to adjust the styles of a specific component, in this case the Button component. The objective is to eliminate the outline surrounding the button when the screen size decreases. The atte ...

Are you looking for straightforward dynamic directives that come with dynamic controllers and a scope?

Feeling like I have a simple problem to solve here. Working within the confines of a TypeScript + Angular application. Within a controller, I've got an array of similar directives that I want to utilize. These are essentially the panels strewn throug ...