firescript / nativescript-contacts

A nativescript module that gives access to the native contact directory.
MIT License
28 stars 32 forks source link

No Accounts! Error message, when no account is configured in device #32

Closed rajasekarsp closed 5 years ago

rajasekarsp commented 7 years ago

Hi,

When my phone is not configured with any account and when i try to add a contact through my app, it throws the following error JS: Error: No Accounts! JS: at Contact.save (file:///data/data/com.bcbsma.myblue2/files/app/tns_modules/nativescript-contacts/contact-model.js:227:19) [angular]

Can you please let us know what happens?

Device: All Android and IOS devices

wern commented 7 years ago

Hi there,

I just experienced the same issue with nativescript-core (version 3.1.2) / nativscript-contacts (version 1.5.3) and the android emulator. iOS is working fine for me.

Looks like the plugin checks for the existence of least one account and throws an error if no accounts are found. As the Android contacts content provider does not need this account, the check could easily been removed and everything is working fine again :).

node_modules/nativescript-contacts/contact-model.android.js line 227ff. :

if (accounts.length === 0) { 
  throw new Error("No Accounts!");
}

accountName = accounts[0].name;
accountType = accounts[0].type;

Just replace this section with

if (accounts.length != 0) { 
   accountName = accounts[0].name;
   accountType = accounts[0].type;
}

and you can add contacts even without creating an account inside your emulator.

Cheers, Werner

steinerj commented 6 years ago

Oldish issue, but since API level 23 you need to check for permission for GET_ACCOUNTS every time you try the operation. So not only do you need these in your manifest:

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

You also need to do something like this (using the great nativescript-permissions plugin):

const contact = new Contact();
(...)
Permissions.requestPermissions([android.Manifest.permission.GET_ACCOUNTS, android.Manifest.permission.WRITE_CONTACTS], "I need these permissions because I'm cool")
            .then(() => {
                contact.save();
            });

I think it's worthwhile putting this in the README (PR follows).