Nativescript-contacts save() hangs


#1

I’m working with the nativescript-contacts and native script-permissions plugins to develop some sample code to simply add a contact on Android. Here’s the sample code:

var contacts = require( "nativescript-contacts" );
var permissions = require( "nativescript-permissions" );

exports.onAddContactsTap = function (args) {
    console.log("Add to Contacts tapped");

    var newContact = new contacts.Contact();
    newContact.name.given = "John";
    newContact.name.family = "Doe";
    newContact.phoneNumbers.push({ label: contacts.KnownLabel.HOME, value: "123457890" }); 
    newContact.phoneNumbers.push({ label: "CELL", value: "11235813" });
    newContact.organization.name = "My Organization";

    permissions.requestPermissions([android.Manifest.permission.GET_ACCOUNTS,
    android.Manifest.permission.READ_CONTACTS,
        android.Manifest.permission.WRITE_CONTACTS],
        "Permission needed to add volunteers to contacts")
        .then(() => {
            console.log("Permission accepted...");
            newContact.save();
            console.log("newContact saved.");
        })
        .catch(() => {
            console.log("Error: " + err);
        });
};

The permissions part works OK, but control is never returned from newContact.save(). I tried using a promise on the call, but with no change in results. This is basically the same code listed on the nativescript-contacts page, but after several hours I’m stuck. Any suggestions?


#2

So, I went back to the original sample code from the nativescript-contacts page (i.e., removed the permissions processing). This time, the command still failed, but I could see the error message, “System.err: Error: No Accounts!” So, I added a google account to the emulator, and now the commands work correctly and I can add a contact.

I’m concluding that on a new emulator image, some amount of initial setup is required (e.g., adding an account under Settings/Accounts).

Is it possible to test for this condition so that a meaningful error message can be displayed?


#3

The plugin already does it for you. You just have to wrap your code with a try/catch block to catch that error in a nice way.


#4

manojdcoder - thanks for the info. I’ll add that.