0

I´m getting an empty object for the admin.firestore.FieldValue.serverTimestamp(), below is the screenshot of the document being stored in Firestore:

enter image description here

Here´s the code for creating the message object (that corresponds to the screenshot above):

import {Message} from "./Message";
import {MessageType} from "./MessageType";
import * as admin from "firebase-admin";

export class TextMessage extends Message {
    constructor(objectID: string,
                content: string,
                authorName: string,
                authorID: string,
                authorPhotoUrl: string,
                createdAt: FirebaseFirestore.FieldValue = admin.firestore.FieldValue.serverTimestamp()) {
        super(objectID,
            content,
            authorName,
            authorID,
            authorPhotoUrl,
            MessageType.text,
            createdAt,)
    }
}

Here´s where I create the TextMessage object:

 const message: TextMessage = new TextMessage(
            messageID,
            "Cualquier consulta escríbeme un mensaje y estaré para ayudarte",
            SUPPORT_NAME,
            SUPPORT_USER_ID,
            defaultProfilePicture
        )

And here´s the Firebase function code in which the code from above is being used:

// Create the chat
    const createChatPromise =
        firestoreDB
            .collection("Chats")
            .doc(chatID)
            .collection("Messages")
            .add(JSON.parse(JSON.stringify(message)))

 // Return the method when both functions are finished
    return Promise.all(
        [
            addToFirestorePromise,
            addToAlgoliaPromise,
            emailPromise,
            createCurrentUserInboxPromise,
            createSupportInboxPromise,
            createChatPromise, ==> This is the promise from above
            addUsersIDsPromise
        ]
    ).catch(error => functions.logger.log("The error is: " + error.message))

From the object´s documentation:

Returns a sentinel used with set(), create() or update() to include a server-generated timestamp in the written data.

Returns: The FieldValue sentinel for use in a call to set(), create() or update().

However, I´m using the add function, I don´t know if this is the reason why is not working.

NOTE: I´m using the following versions for firebase admin and functions:

"firebase-admin": "^8.10.0",
"firebase-functions": "^3.7.0",
Andrey Solera
  • 2,311
  • 2
  • 26
  • 51
  • I don't see where you use TextMessage here. if you never construct an object of that type, then its constructor never runs. Please edit the question to describe in detail what you expect to happen with this code, and why. – Doug Stevenson Oct 04 '21 at 07:01
  • @DougStevenson I´ve updated the question to contain how I am constructing the text message object. – Andrey Solera Oct 04 '21 at 14:32

1 Answers1

1

It's not clear to me why you are using both stringify and parse together like this:

JSON.parse(JSON.stringify(message))

The stringify will destroy the token that internally represents the server timestamp. You're supposed to pass the object to store in Firestore directly to add() or set() - never stringify it.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441