Reputation: 2374
I am using ionic-datepicker (ionic -datepicker) to let user pick a time. I have this code in my controller to return the val user picked:
$scope.datePickerCallback = function (val) {
if(typeof(val)==='undefined'){
console.log('Date not selected');
}else{
console.log('Selected date is : ', val);
$scope.pickedDate = val;
}
};
Then I try to $add the task object (contain this val in one of its property like task.date) into an $firebaseArray. My firebase keep return a permission denied:
FIREBASE WARNING: set at /tasks/-JwIkrTLg5XvrhjLVRTJ failed: permission_denied
I console log it before add into firebase. The output of the date property is:
date: Fri Aug 21 2015 00:00:00 GMT-0700 (PDT) The proto: Invalid Date.
So how to save a date object into Firebase? Do I need to set some rules for that? Or do I need to do something with the val to save it. One thing I need to do is to have the val like Firebase.ServerValue.TIMESTAMP. So I can convert the date value into different readable time format, do time calculation and all sort of stuffs. I can convert the val to String and save successfully in Firebase. But I will lost all the functionality a date object can do (like with moment.js with the return value)
Upvotes: 0
Views: 1073
Reputation: 598765
Firebase can only store valid JSON types and a JavaScript Date
is not such a type.
There are two common reasons you want to store a date/time:
If your reason for storing the date is that you want to display it later, you'll want to store it as a string:
task.date = val.toString();
If you want to store the date so that you can later compare it to other dates (or get all tasks in a time period), you'll want to store the so-called timestamp of the date:
task.timestamp = val.getTime();
The Date.getTime()
function of JavaScript returns the number of milliseconds since 1 January 1970 00:00:00 UTC.
If you use different properties for both types, you can store both a textual display value and a numeric value that you use for sorting. Alternative you can reconstruct the Date
from the timestamp with:
new Date(task.timestamp)
Upvotes: 3