I'm trying to push the values into my firebase-database. When I try pushing the values it gives me an error in the console (Error: Reference.push failed: first argument contains a function in property). I looked it up on firebase and found out it was because of a function being pushed, but i'm not pushing any function at all. But I have a feeling that it might be because of datetimepicker. Need help!
MY JS
/*global angular*/
var app = angular.module('downtime', ['ngRoute', 'firebase']);
app.config(['$routeProvider', function ($routeProvider) {
'use strict';
$routeProvider.when('/downtime', {
templateUrl: 'downtime/downtime.html',
controller: 'downtimeCtrl'
});
}]);
app.controller('downtimeCtrl', ['$scope', '$firebaseObject', '$firebaseArray', function ($scope, $firebaseObject, $firebaseArray) {
'use strict';
$scope.allEquipments = [];
$scope.allSystems = [];
$scope.manageDowntime = function () {
var doesExist = false;
angular.forEach ($scope.data , function (d) {
angular.forEach (d.equipments, function (e) {
})
});
firebase.database().ref('downtime/' + $scope.equipment + '/downtime').push({
equipment: $scope.equipment,
type : $scope.type,
start: $scope.startDT,
end: $scope.endDT
});
};
var ref = firebase.database().ref();
var data = ref.child("data");
var list = $firebaseArray(data);
list.$loaded().then(function(data) {
$scope.data = data;
angular.forEach ($scope.data , function (d) {
$scope.allSystems.push(d.$id);
angular.forEach (d.equipments, function (e) {
$scope.allEquipments.push(e.equipment);
console.log($scope.allEquipments);
})
});
console.log($scope.data);
}).catch(function(error) {
$scope.error = error;
});
$('#datetimepicker1').datetimepicker();
$('#datetimepicker2').datetimepicker();
}]);
app.directive('customzdatetime', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
element.datetimepicker({
debug: false,
format: 'DD-MM-YYYY hh:mm'
}).on('dp.change', function (e) {
ngModelCtrl.$setViewValue(e.date);
scope.$apply();
});
}
};
});
My HTML
<div class="page-header">
<h1>MANAGE DOWNTIME </h1>
</div>
<div data-ng-app="downtime">
<div class="row">
<div class="col-xs-12">
<div class="form-group col-xs-6 col-xs-offset-3" id="equipList">
<label for="selectequ">Select Equipment</label>
<select class="form-control" id="selectequ" data-ng-model="equipment"
>
<option data-ng-repeat="eq in allEquipments" >{{eq}}</option>
</select>
{{equipment}}
</div>
<div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-6 col-xs-offset-3" id="Type">
<label for="searchType">Search by Type:</label>
<select class="form-control" id="searchType" data-ng-model="type">
<option value="" disabled="" selected="" style="display: none">Select type of maintenance</option>
<option>Corrective Maintenance</option>
<option>Preventive Maintenance</option>
<option>Standby</option>
</select>
</div>
{{type}}
</div>
</div>
<div class="row">
<div class="form-group col-xs-6 col-xs-offset-3">
<label for="date">Start of Downtime</label>
<!-- <input type="datetime-local" id="datetimepicker1" class="form-control" placeholder="Day, Month, Year" data-ng-model="startDT"/>-->
<input type="text" class="form-control" placeholder="Day, Month, Year" data-ng-model="startDT" customzdatetime />
</div>
{{startDT}}
<div class="form-group col-xs-6 col-xs-offset-3">
<label for="date">End of Downtime</label>
<input type="text" class="form-control" placeholder="Day, Month, Year" data-ng-model="endDT" customzdatetime/>
{{endDT}}
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6" id="central">
<a class="btn cd-add-to-cart cd-add-to-cart:hover cd-add-to-cart:active" role="button" data-ng-click="manageDowntime()">MANAGE <span class="glyphicon glyphicon-tasks"></span></a>
</div>
</div>
</div>
firebase will not accept date as datatype, try to convert them as string or number and then try to push it into the firebase. I hope your problem is the $scope.startDT because it contains some function in it, your're trying to use it directly that is why it throws a error, try to convert them as string or number, then try to push.
firebase.database().ref('downtime/' + $scope.equipment + '/downtime').push({
equipment: $scope.equipment,
type : $scope.type,
start: new Date($scope.startDT).toLocaleString(),
end: new Date($scope.endDT).toLocaleString()
or
start: new Date($scope.startDT).getTime(), //this will get you unix timestamp as number
end: new Date($scope.endDT).getTime()
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With