0

I want to add a timestamp as a custom key in firebase however I can't add it since it always gives me an error.

This is how I'm inserting into the database.

db.ref('items/').child(timestamp).set({
       name: item,
       type: type,
   })

This is how I get the timestamp: firebase.database.ServerValue.TIMESTAMP

And the error I get is Reference.child failed: First argument was an invalid path= "[object Object]". Paths must be non-empty strings and can't containt ., #, $, [ or ].

I don't understand what is going on here and if I add that same timestamp as a value of one child it adds it without and problem

  • You can't use Firebase's server-side timestamp as a key. See https://stackoverflow.com/questions/47274974/how-to-use-firebases-servervalue-timestamp-as-a-child-and-set-a-value-to-it – Frank van Puffelen Apr 15 '19 at 14:38

1 Answers1

1

As explained in the doc, the value you get with firebase.database.ServerValue.TIMESTAMP is an Object but you have to pass a String to the child() method.

This is what the error message indicates:

Reference.child failed: First argument was an invalid path= "[object Object]". Paths must be non-empty strings and can't containt ., #, $, [ or ].

You may generate the timestamp in your front end and pass it to the child() method, see How do you get a timestamp in JavaScript?


In addition, note that ServerValue.TIMESTAMP is "a placeholder value for auto-populating the current timestamp... as determined by the Firebase servers" . It means that you cannot call a JavaScript method (e.g. getTime()) on it, in your front-end.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121