如何在 v2 onDocumentWritten 云函数中使用 getCountFromServer?

编程入门 行业动态 更新时间:2024-10-04 23:33:33

如何在 v2 onDocumentWritten 云<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数中使用 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?

本文发布于:2024-05-31 01:12:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771118.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   如何在   onDocumentWritten   getCountFromServer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!