Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase.database() Date is not submitted

I have tried to create a record in the firebase real-time database from an AngularJS web application.

Code

firebase.database().ref("XYZ").push({
      name: 'name',
      contentType: 'video/mp4',
      videoUrl:downloadURL,
      createdDate: new Date(),
      uploadedBy: '[email protected]'
})

Problem

Record created successfully but createdDate field will not added in database. what is the issue?

like image 618
Jitendra Tiwari Avatar asked Jun 30 '16 07:06

Jitendra Tiwari


1 Answers

It doesn't work because you are trying to push a Date object instead of a timestamp/string. You should use:

  • Date() to get a string like: Thu Jun 30 2016 09:40:54 GMT+0200 (CEST)
  • new Date().getTime() or Date.now() to get a timestamp like 461467272336700

If you are looking for a nicer solution you should let Firebase set that value with its current date using firebase.database.ServerValue.TIMESTAMP

like image 78
Devid Farinelli Avatar answered Sep 27 '22 16:09

Devid Farinelli