0

i have been searching for solution for 2 days now here is my code

saveData = () =>{
    let servertime = Firebase.database.ServerValue.TIMESTAMP;
    this.itemsRef.push({name:this.state.text,date:servertime});
    var s = new Date(parseInt(servertime)).toUTCString();
    alert(s);
  }

but alert return me with invalid value i try to put an unix interger directly like this

saveData = () =>{
        let servertime = Firebase.database.ServerValue.TIMESTAMP;
        this.itemsRef.push({name:this.state.text,date:servertime});
        var s = new Date(1509004701954).toUTCString();
        alert(s);
      }

and it work well why cant i get the same result?

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Billy Koswara
  • 457
  • 1
  • 5
  • 21

1 Answers1

2

Firebase.database.ServerValue.TIMESTAMP is a constant that represents the object { ".sv": "timestamp" }. Firebase uses it to automatically put in the current timestamp.

You need to push the data, then grab the snapshot and read the 'date' back:

saveData = () => {
   this.itemsRef.push(
       {
           name:this.state.text, 
           date:Firebase.database.ServerValue.TIMESTAMP
       }).then((snapshot) => {
           var s = new Date(parseInt(snapshot.val().date)).toUTCString();
           alert(s);
       }
 }
Jeremy
  • 3,438
  • 3
  • 34
  • 57