I´m getting an empty object for the admin.firestore.FieldValue.serverTimestamp()
, below is the screenshot of the document being stored in Firestore:
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",