Error throwed by nativescript-contacts


#1

I try use this plugins: https://www.npmjs.com/package/nativescript-contacts
I have this code to add contact to contact list:

const newContact = new contacts.Contact();
        newContact.name.displayname = "test";
        newContact.name.given = "petr";
        newContact.name.family = "danek"
        newContact.phoneNumbers.push({ label: contacts.KnownLabel.HOME, value: "777666888" });
        try {
            newContact.save();
            console.log("saved");
        } catch (e) {
            console.log("error");
            console.dir(e);
        }

I test it on android emulator but I got error:
“originalStack”: "Error: No Accounts!\n at ZoneAwareError (file:///data/data/org.nativescript.SkautISmobile/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:994:
33)\n at Contact.save (file:///data/data/org.nativescript.SkautISmobile/files/app/tns_modules/nativescript-contacts/contact-model.js:259:19)\n throwed in line with newContact.save()

What I have wrong. It could be according to docs.
Thanks


#2

You haven’t created a account yet, login with Google as you would do in real device if play services are installed or create a dummy account.


#3

Thanks for answer,
Can you describe it more detailed?
I think that i am login in emulator with my gmail. Because I see some contacts from gmail in contacts app. For example if i open google play store, I am login there.
Or how i should login create dummy account?


#4

That sounds like you already have the accounts in place but did you acquire the permissions?


#5

Yea, thanks for tips.
I add permission and request permission. But now after call newContact.save(); I get Error:
“Error: No Accounts!
at ZoneAwareError (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:994:33)
at Contact.save (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-contacts/contact-model.js:259:19)
at file:///data/data/org.nativescript.testapp/files/app/services/shared/phone-contact.service.js:107:24
at ZoneDelegate.invoke (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:365:26)
at Object.onInvoke (file:///data/data/org.nativescript.SkautISmobile/files/app/tns_modules/@angular/core/bundles/core.umd.js:4787:33)
at ZoneDelegate.invoke (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:364:32)
at Zone.run (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:125:43)
at file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:761:57
at ZoneDelegate.invokeTask (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:398:31)
at Object.onInvokeTask (file:///data/data/org.nativescript.testapp/files/app/tns_modules/@angular/core/bundles/core.umd.js:4778:33)
at ZoneDelegate.invokeTask (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:397:36)
at Zone.runTask (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:165:47)
at drainMicroTaskQueue (file:///data/data/org.nativescript.testapp/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:593:35)”
My code now looks like this:

...
Permissions.requestPermissions([android.Manifest.permission.GET_ACCOUNTS, android.Manifest.permission.WRITE_CONTACTS], "This permissions are required for save contact to mobil contacts.")
            .then(() => {
                console.log("ok");
                newContact.save();
            }).catch(e => {
                console.log("error");
                console.error(e);
        });

I find same error on git: https://github.com/firescript/nativescript-contacts/issues/32, but there is only solution in node_modules code, what is not best solution. What is wrong? I have to somehow add accounts? but if i open contacts app there is about 20 contact from my gmail. Thanks


#6

I found this to be a common error on new Android emulators. On the emulator, log into Google, and separately, create at least one contact. See if that helps.


#7

Thanks for tips, but not help it
I go to contacts in emulator i was under my gmail and create new contact. I see it in contacts, but If I want create new cotnact using plugin I get same error: No account.

But if it is only emulator problem, its ok