Ad

ReferenceError: Timestamp Is Not Defined

I am trying to convert Firebase TimeStamp into JavaScript date.

I referred this link to convert Javascript object to Javascript date, https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp

Firestore timestamp (From API):

"time": {
       "_seconds": 1563620755,
        "_nanoseconds": 688000000
 }

Before saving time into cloud firestore I've to convert it to JavaScript Date().

So I used below code to convert object into Date.

let data = request.body
let tiemStamp = new Timestamp(data.time._seconds,data.time._nanoseconds)

But I got below error,

ReferenceError: Timestamp is not defined

How do to solve it? Thanks!

Ad

Answer

As Timestamp is under firebase.firestore namespace (or in Javascripts case object), you might want to use it as follows:

let tiemStamp = new firebase.firestore.Timestamp(data.time._seconds,data.time._nanoseconds)

console.log(new firebase.firestore.Timestamp());
<script src="https://www.gstatic.com/firebasejs/6.3.5/firebase.js"></script>

Ad
source: stackoverflow.com
Ad