App Crashing on contact.save() when using ID - Apache Cordova

735 views Asked by At

Edit: I have tried this on multiple APIs and also an actual android device and it still crashes on save.

I'm developing a contact based app on android using Tools for Apache Cordova for Visual Studio and the cordova contacts plugin (v2.13). I can currently save and retrieve contacts but I cannot UPDATE a contact. I am using the genymotion emulator. and the devices native contact list.

You are supposed to be able to update a contact by passing in the a contact ID that already exists.

I have the contact object and all the fields are being populated but the app just crashes with no error messages when I try to save it.

I am aware this WAS an issue in contacts v1.3 and lower but it has been marked as resolved on their issues list.

Contact Issue Resolution Documentation

Here is a screenshot of my object and also the full AngularJS code

Object Image

droidSync.controller('managerController', function ($scope) {

    //Initialize model
    $scope.contact = {};

    // Create a new contact
    $scope.createContact = function () {
        // Contact Object
        contact = navigator.contacts.create();
        $scope.saveContact();
    };

    // Pick Contact from Device
    $scope.editContact = function () {
       contact = navigator.contacts.pickContact(function(contact){
            console.log(contact);

            //This only updates the text fields. It does not actually assign new values to the object
            $scope.$apply(function () {
                $scope.contact.firstName = contact.name.givenName;
                $scope.contact.lastName = contact.name.familyName;
                $scope.contact.mobileNo = contact.phoneNumbers[0].value;
                $scope.contact.homeNo = contact.phoneNumbers[1].value;
                $scope.contact.email = contact.emails[0].value;
                $scope.contact.id = contact.id;
            });
        })
    };

    // Delete Contact from Device
    $scope.deleteContact = function () {

        //Delete Function Here

    };

    $scope.saveContact = function () {

        var contact = navigator.contacts.create();

        if ($scope.contact.id !== "undefined") {
            contact.id = $scope.contact.id;
        }

        // Display Name and Email
        contact.displayName = $scope.contact.firstName;
        contact.nickname = $scope.contact.lastName;

        var emails = [];
        emails[0] = new ContactField('work', $scope.contact.email, true)
        contact.emails = emails;

        // Phone Numbers
        var phoneNumbers = [];
        phoneNumbers[0] = new ContactField('mobile', $scope.contact.mobileNo, true); // preferred number
        phoneNumbers[1] = new ContactField('home', $scope.contact.homeNo, false);
        contact.phoneNumbers = phoneNumbers;

        // Names
        var name = new ContactName();
        name.givenName = $scope.contact.firstName;
        name.familyName = $scope.contact.lastName;
        contact.name = name;

        // save to device
        contact.save();
    }
});

droidSync.controller('settingsController', function ($scope) {

});

1

There are 1 answers

0
Gary McNeill On BEST ANSWER

Well turns out it was the main scripts file being loaded in the wrong place... I had it referenced in the <head> tag and it needed to go in where the cordova references go at the end of the <body>

<!-- DroidSync references -->
<!-- WHERE IT WAS -->
    <link href="css/index.css" rel="stylesheet" />
    <link href="css/ionicons.css" rel="stylesheet" />
    <link href="Content/ionic.css" rel="stylesheet">
</head>

<!-- Cordova reference, this is added to your app when it's built. -->
    <script src="app/scripts.js"></script> <!-- wHERE IT WAS SUPPOSED TO GO -->
    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>
    <script src="scripts/index.js"></script>