0

I'm having a problem with my Crud based ionic App with Firebase as backend. I have item.service which creates and writes object to Firebase, one of the properties is date object, which is added with input tag type=date, native. im ok with that at the, moment. Problem comes, when I try to update date property of my item, Im using ionic-datepicker by this fella (http://rajeshwarpatlolla.github.io/DatePickerForIonicFramework/demo/ "Demo").

in my controller code im calling $save function which saves/updates $scope properties to item.

           $scope.updateItem = function () {

                $scope.item.$save({
                    name: $scope.item.name
                    , serial: $scope.item.serial
                    , hirestart: $scope.item.hirestart.getTime()
                    , hireend: $scope.item.hireend.getTime()
                    , orderNumber: $scope.item.ordernumber
                    , category: $scope.item.category
                });
                //-------------------------
                //  **After making selection in date picker console logs value which is returned from datepicker. But nothing is pushed to Firebase data base.** 

//********************************************** U-P-D-A-T-E****** for some reason I copied code above without, .getTime() attached to date objects, anyway I did try getTime() and valueOf() what it does it gets value in to the $scope but when I click Save the date fields in the form gets blank and transition to home.state. I tried console log inside save function and it logs value but nothing saved. //***************************************************************8

                console.log($scope.item.hirestart.valueOf()); 


                $state.go('menu.plantRegister');

            };

Below is edit.html from which datepicker called.

            <li class="item dark item-stacked-label item-icon-right">
                <ion-note item-right class="calm"> Hire start date:</ion-note>
                <label>{{item.hirestart | date:'dd-MMMM-yyyy'}}</label>
                <i class="icon ion-ios-calendar-outline calm" ng-click="openStart()"></i>
            </li>

            <li class="item dark item-stacked-label item-icon-right">
                <ion-note item-right class="calm"> Hire end date:</ion-note>
                <label>{{item.hireend | date:'dd-MMMM-yyyy'}}</label>
                <i class="icon ion-ios-calendar-outline calm" ng-click="openEnd()"></i>
            </li>

And here's datepicker methods.

            $scope.openStart = function (val) {
                var ipObj1 = {
                    callback: function (val) { //Mandatory
                        console.log('Return value from the datepicker popup is : ' + val, new Date(val));
                        $scope.item.hirestart = new Date(val);
                    }
                    , disabledDates: []
                    , from: new Date(2012, 1, 1)
                    , to: new Date(2016, 10, 30)
                    , inputDate: new Date()
                    , mondayFirst: true
                    , showTodayButton: true
                    , disableWeekdays: [0]
                    , closeOnSelect: false
                    , templateType: 'modal'
                };
                ionicDatePicker.openDatePicker(ipObj1);
            };

            $scope.openEnd = function (val) {
                var ipObj1 = {
                    callback: function (val) { //Mandatory
                        console.log('Return value from the datepicker modal is : ' + val, new Date(val));
                        $scope.item.hireend = new Date(val);
                    }
                    , disabledDates: []
                    , from: new Date(2012, 8, 2)
                    , to: new Date(2016, 8, 25)
                    , inputDate: new Date()
                    , mondayFirst: true
                    , showTodayButton: true
                    , closeOnSelect: false
                    , templateType: 'modal'
                };
                ionicDatePicker.openDatePicker(ipObj1);
            };
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • have you tried debugging it with some logs? try to log `$scope.item.hirestart` after setting the new value in your callback. – adolfosrs Jun 13 '16 at 23:14
  • The Firebase Database stores JSON values. While `Date` is a valid JavaScript type, there is no spec for it in JSON. You'll have to serialize/deserialize it in your app. See http://stackoverflow.com/questions/30021133/how-do-you-save-a-date-field-in-firebase-using-angularfire – Frank van Puffelen Jun 14 '16 at 01:13
  • I do apologize for attaching incorrect code, i did try $scope.item.hirestart with valueOf() and getTime() and when i console log it i get returned value of $scope.item.hirestart.valueOf() as unix timestamp, and i tried to log it inside save function , before $save method and after. i always get value, but it never gets saved to database, i just cant get sense of it. – Andy Piotrowicz Jun 14 '16 at 07:14
  • I have solved the problem, thanks for all of your comments, what i did was: in date picker method `$scope.openStart = function (val) { var ipObj1 = { callback: function (val) { //Mandatory console.log('Return value from the datepicker popup is : ' + val, new Date(val)); $scope.item.hirestart = new Date(val); }` , i assigned `$scope.item.hirestart = val`, rather than `newDate(val)`, and then in firebase `$save` i just save `$scope.item.hirestart` without calling` toString() or getTime(), or valueOf()`. – Andy Piotrowicz Jun 15 '16 at 09:43

0 Answers0