Wednesday, June 1, 2016

error JSON.stringify()ing argument: RangeError: Invalid Date

There is minor bug in contact plugin in cordova ionic. contact list is loaded with error due to the birthdate field in contact card. Its bad practice to keep minor error in execution of code, It may effect to different OS versions.

error JSON.stringify()ing argument: RangeError: Invalid Date


Cordova Plugin: cordova-plugin-contacts
Verison: 2.1.0 "Contacts"


Open convertUtils.js file  

File Path: plugins/cordova-plugin-contacts/www/convertUtils.js



Find function "toCordovaFormat" and remove below try-catch code.


 try {
         contact.birthday = new Date(parseFloat(value));
} catch (exception){
          console.log("Cordova Contact toCordovaFormat error: exception creating date.");
}

add below code instead of try-catch

if (value !== null) {
        try {
                contact.birthday = new Date(parseFloat(value));
               
                //we might get 'Invalid Date' which does not throw an error
                //and is an instance of Date.
                if (isNaN(contact.birthday.getTime())) {
                       contact.birthday = null;
               }
               
        } catch (exception){
                console.log("Cordova Contact toCordovaFormat error: exception creating date.");
        }
}

Check below screenshot for better understand.




iOS contact card does not have value for "displayName". "displayName" is used for the android devices. So For iOS device, use "name.formatted" instead of "displayName".

Cheers.... Keep Coding... :)