函数中使用 getCountFromServer?"/>
如何在 v2 onDocumentWritten 云函数中使用 getCountFromServer?
我正在尝试编写具有此标头的服务器端 v2 云函数(触发器)...
exports.userUpdatedTrigger = onDocumentWritten("users/{userId}", async (event) => {
作为此功能的一部分,我想使用新的 getCountFromServer 功能进行计数,但不需要读取所有文档即可。
const countQuery = query(
collection(db, "users"),
where("account_id", "==", accountId),
)
const result = await getCountFromServer(countQuery)
问题是,虽然我可以部署它,但当我更新用户记录(触发 onDocumentWritten)时,我在云控制台中收到此触发器的错误消息。
错误是:
TypeError: collection is not a function
我用的进口货是:
const {
FieldValue,
getFirestore,
getCountFromServer,
query,
collection,
where,
} = require("firebase-admin/firestore")
我在 firebase doco 中找不到哪些包包含“查询”、“集合”和“哪里”功能。
有没有人能够让这两个东西一起工作,onDocumentWritten + getCountFromServer?
或者,有没有人能够让 getCountFromServer 在云函数中正常工作?我可以让它在客户端工作,但不能在服务器端工作。在我尝试添加 getCountFromServer
之前,我确实设法让 onDocumentWritten 以简单的形式工作见上文。
我希望能够在云函数中使用 getCountFromServer。我可以使用 v9 API 方法让它在客户端工作,但不能让它在服务器端工作。
回答如下:在服务器端代码中,该函数称为
count()
,而不是getCountFromServer()
:
const query = db
.collection("users")
.where("account_id", "==", accountId)
const snapshot = await query.count().get()
console.log("count", { count: snapshot.data().count })
更多推荐
如何在 v2 onDocumentWritten 云函数中使用 getCountFromServer?
发布评论