如何从 .onCreate 函数更改 Firestore 文档中的值并将它们 return 给用户

How can I change values in a Firestore document from the .onCreate function and return them to a user

我正在尝试 change/create 来自 firebase 云函数的 firestore 文档中的一些值,但无法弄清楚,可能很容易遗漏一些东西。

exports.onEnterDetails = functions.firestore.document('accounts/{accountId}')
    .onCreate((snapshot, context) => {
        // grab values from the document
        const values = snap.data();
        // do stuff with the values the user put in
        ...
    })

如何更改同一个 firestore 文档中的 value/create 个新条目,以及如何 return 在前端(网站)向用户提供其中的一些新条目?

在 Firestore 中创建文档后,您的 onCreate Cloud Function 被异步触发。此时无法再更改文档中的初始数据,也无法将 return 数据发送到创建文档的客户端。

如果你想要一个在写入数据之前修改数据的同步流程,并且可以响应客户端,请考虑实现一个 Callable Cloud Function or a HTTP cloud function,两者都是同步的。在其中任何一个中,您都可以从应用程序代码中调用 Cloud Function 并通过该调用传递数据,而不是直接将其写入 Firestore。 Cloud Functions 然后调用 Firestore,returns 向调用者提供必要的结果。