【发布时间】:2025-12-23 04:35:05
【问题描述】:
我正在为admin.firestore.FieldValue.serverTimestamp() 获取一个空对象,下面是存储在 Firestore 中的文档的屏幕截图:
这是创建消息对象的代码(对应于上面的屏幕截图):
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,)
}
}
这里是我创建 TextMessage 对象的地方:
const message: TextMessage = new TextMessage(
messageID,
"Cualquier consulta escríbeme un mensaje y estaré para ayudarte",
SUPPORT_NAME,
SUPPORT_USER_ID,
defaultProfilePicture
)
这是使用上述代码的 Firebase 函数代码:
// 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))
来自对象的文档:
返回与 set()、create() 或 update() 一起使用的标记,以在写入的数据中包含服务器生成的时间戳。
返回: 用于调用 set()、create() 或 update() 的 FieldValue 标记。
但是,我正在使用 add 函数,我不知道这是否是无法正常工作的原因。
注意:我正在使用以下版本的 firebase 管理员和功能:
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.7.0",
【问题讨论】:
-
我看不到你在这里使用 TextMessage 的地方。如果您从不构造该类型的对象,则其构造函数将永远不会运行。请编辑问题以详细描述您希望使用此代码发生的情况以及原因。
-
@DougStevenson 我已经更新了问题以包含我如何构建文本消息对象。
标签: node.js firebase server google-cloud-functions firebase-admin