@ewehrmann wrote:
I am trying to write a function that will save a Contact to the device using the Ionic Native Contacts API. First time working with the Contact api.
I am able to get an instantiation of ‘contact’ but when I test in the browser I get an error ‘Cannot set property ‘name’ of undefined’. I also tried to compile and install an IPA file on my iOS device. The code compiled without error but when I trigger the function it does not add the contact to the device.
Two questions:
Should I be able to test this API from browser without an error??? e.g. when I invoke the Call or SMS functions from browser they do not cause the app to error and stop, I just get get a message saying “cordova_not_available”
Is there a problem with the syntax or logic in my code?
From home.ts page
import { Contacts, Contact, ContactField, ContactName } from ‘@ionic-native/contacts’;
constructor(public navCtrl: NavController, public navParams: NavParams, public search: SearchProvider, private callNumber: CallNumber, private sms: SMS, private contacts: Contacts, public alertController: AlertController) {
this.employees$ = ;
this.letters$ = ;
}saveContact(fullName, phoneNumber) {
var first = fullName[0].split(" “)[0];
var last = fullName[0].split(” ")[1];
let contact: Contact = this.contacts.create();
console.log(contact[0])
contact.name = new ContactName(null, first, last);
contact.save().then(
() => this.presentAlert(),
(error: any) => console.error(‘Error saving contact.’, error)
);
}From app.module.ts
import { Contacts} from ‘@ionic-native/contacts’;
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
SearchProvider,
CallNumber,
Contacts,
HTTP,
SMS,
{provide: HttpBackend, useClass: NativeHttpFallback, deps: [Platform, NativeHttpBackend, HttpXhrBackend]}
]
Posts: 1
Participants: 1