Firestore write Timestamp Field

I want to write a timestamp field into a Firestore document in a typescript project.

import * as admin from 'firebase-admin'

const myDoc = {
     myDate: admin.firestore.Timestamp.fromDate(new Date()),
    }

    const myDocument = await admin
        .firestore()
        .collection('col')
        .doc('myId')
        .set(myDoc)

Unfortunately, the generated document contains for MyDate a string field and not a date/time field as expected. What am I doing wrong?

Upvotes: 0

Views: 588

Answers (1)

Update 15-June-2021:

For write a new document with set use this:

const MyDoc = {
  MyDate: new Date(),
}

For update an existing document with update use this:

const MyDoc = {
  MyDate: admin.firestore.Timestamp.now(),
}

const myDocument = await admin
    .firestore()
    .collection('col')
    .doc('myId')
    .update(myDoc)

I cannot explain why there is a difference.

Upvotes: 0

Related Questions