在 Cloud Firestore 上添加文档时更新字段

Lhe*_*air 1 node.js firebase google-cloud-firestore

我有一个关于在我的 Android 应用程序中使用 Cloud Firestore 函数的问题(我正在使用 Android Studio 在 kotlin 上写作)

阅读了一些文档后,我认为在我的数据库中创建新文档时,可以在 Firestore 函数中运行自定义方法。我需要做的就是更新一个字段。

问题是 Cloud Firestore 上的这些函数需要用 JavaScript 编写,而我需要使用 Node.js,而我对此的了解为 0。

对于具有 Cloud Firestore 知识的任何开发人员,有关此问题的任何指南或提示?

Fer*_*ndo 7

问题是 Cloud Firestore 上的这些函数需要用 JavaScript 编写,而我需要使用 Node.js,而我对此的了解为 0。

不知道它是否有帮助,但 Cloud Functions 也可以用 Python 或 Go 编写。您可以在此处查看有关当前运行时和语言的更完整信息。

但是让我们试着回答你的问题,好吗?我将在下面的示例中使用 Node.js 8 运行时。

介绍

Google Cloud Functions 目前支持两种类型的函数:

  1. HTTP 函数,由简单的 HTTP 请求触发,以及
  2. 后台函数,由来自 Google Cloud 基础架构的事件触发,例如 Cloud Firestore 事件。这就是您所需要的,所以让我们专注于此。

设置

由于您使用的是 Cloud Firestore,我假设您已经设置了一个 Firebase 项目。因此,第一步(如果您还没有)是安装Firebase CLI并按照其说明在本地设置您的项目。当被问到时,选择“功能:配置和部署云功能”选项以启用它,并选择“使用现有项目”来选择您的项目。

$ firebase login
$ firebase init
Run Code Online (Sandbox Code Playgroud)

完成设置后,您的目录中基本上将具有以下结构:

firebase.json
.firebaserc
functions/
    index.js
    package.json
Run Code Online (Sandbox Code Playgroud)

现在,在您开始编码之前,您应该了解一些有关 Cloud Firestore 事件的信息。其中有 4 个(列表在这里):

  • onCreate:第一次写入文档时触发。
  • onUpdate:当文档已经存在并且有任何值改变时触发。
  • onDelete:删除带有数据的文档时触发。

  • onWrite:当 onCreate、onUpdate 或 onDelete 被触发时触发。

由于您只需要捕获一个创建事件,因此您将编写一个onCreate事件。

编码

为此,请打开functions/index.js文件并键入以下代码:

const functions = require('firebase-functions');

// this function is triggered by "onCreate" Cloud Firestore events
// the "userId" is a wildcard that represents the id of the document created inside te "users" collection
// it will read the "email" field and insert the "lowercaseEmail" field
exports.onCreateUserInsertEmailLowercase = functions.firestore
    .document('users/{userId}')
    .onCreate((snapshot, context) => {
        // "context" has info about the event
        // reference: https://firebase.google.com/docs/reference/functions/cloud_functions_.eventcontext
        const { userId } = context.params;

        // "snapshot" is a representation of the document that was inserted
        // reference: https://googleapis.dev/nodejs/firestore/latest/DocumentSnapshot.html
        const email = snapshot.get('email');

        console.log(`User ${userId} was inserted, with email ${email}`);

        return null;
    });
Run Code Online (Sandbox Code Playgroud)

您可能会猜到,这是一个非常简单的 Cloud Functions 函数,它只记录文档的 id 及其“电子邮件”字段。所以现在我们进入问题的第二部分:我们如何编辑这个新创建的文档?这里有两个选项:(1) 更新您刚刚创建的文档和 (2) 更新其他文档,因此我将其分为 2 个部分:

(1) 更新刚刚创建的文档

答案在于“快照”参数。尽管它只是您插入的文档的表示,但它在其中包含一个DocumentReference,这是一种不同类型的对象,具有读取、写入和侦听更改功能。让我们使用它的set方法插入新字段。所以让我们改变我们当前的函数来做到这一点:

const functions = require('firebase-functions');

// this function is triggered by "onCreate" Cloud Firestore events
// the "userId" is a wildcard that represents the id of the document created inside te "users" collection
// it will read the "email" field and insert the "lowercaseEmail" field
exports.onCreateUserInsertEmailLowercase = functions.firestore
    .document('users/{userId}')
    .onCreate((snapshot, context) => {
        // "context" has info about the event
        // reference: https://firebase.google.com/docs/reference/functions/cloud_functions_.eventcontext
        const { userId } = context.params;

        // "snapshot" is a representation of the document that was inserted
        // reference: https://googleapis.dev/nodejs/firestore/latest/DocumentSnapshot.html
        const email = snapshot.get('email');

        console.log(`User ${userId} was inserted, with email ${email}`);

        // converts the email to lowercase
        const lowercaseEmail = email.toLowerCase();

        // get the DocumentReference, with write powers
        const documentReference = snapshot.ref;

        // insert the new field
        // the { merge: true } parameter is so that the whole document isn't overwritten
        // that way, only the new field is added without changing its current content
        return documentReference.set({ lowercaseEmail }, { merge: true });
    });
Run Code Online (Sandbox Code Playgroud)

(2) 从另一个集合更新一个文档

为此,您需要将firebase-admin添加到您的项目中。它具有所有管理员权限,因此您可以写入项目内的任何 Cloud Firestore 文档。

functions目录中,运行:

$ npm install --save firebase-admin
Run Code Online (Sandbox Code Playgroud)

由于您已经在 Google Cloud 的基础架构中,因此初始化它就像将以下几行添加到index.js文件中一样简单:

const admin = require('firebase-admin');
admin.initializeApp();
Run Code Online (Sandbox Code Playgroud)

现在您所要做的就是使用 Admin SDK 获取您希望更新的文档的 DocumentReference,并使用它来更新其中的一个字段。

在本例中,我将考虑您有一个名为的集合stats,其中包含一个users文档,其中包含一个counter跟踪users集合中文档数量的文档:

// this updates the user count whenever a document is created inside the "users" collection
exports.onCreateUpdateUsersCounter = functions.firestore
.document('users/{userId}')
.onCreate(async (snapshot, context) => {
    const statsDocumentReference = admin.firestore().doc('stats/users');

    // a DocumentReference "get" returns a Promise containing a DocumentSnapshot
    // that's why I'm using async/await
    const statsDocumentSnapshot = await statsDocumentReference.get();
    const currentCounter = statsDocumentSnapshot.get('counter');

    // increased counter
    const newCounter = currentCounter + 1;

    // update the "counter" field with the increased value
    return statsDocumentReference.update({ counter: newCounter });
});
Run Code Online (Sandbox Code Playgroud)

就是这样!

部署

但是现在您已经有了编码部分,您如何部署它以使其在您的项目中运行,对吗?让我们再次使用 Firebase CLI 来部署新的 Cloud Functions。

在项目的根目录中,运行:

$ firebase deploy --only functions:onCreateUserInsertEmailLowercase
$ firebase deploy --only functions:onCreateUpdateUsersCounter                                 
Run Code Online (Sandbox Code Playgroud)

这几乎是基础知识,但如果您愿意,可以查看其文档以获取有关部署 Cloud Functions 的更多信息。

调试

好的,是的,但是我们怎么知道它有效呢?转到https://console.firebase.google.com并尝试一下!插入几个文件,看看神奇的发生。如果您需要进行一些调试,请单击左侧的“功能”菜单,您将能够访问您的功能日志。

这几乎是您的用例场景,但如果您想更深入地了解 Cloud Functions,我真的推荐它的文档。它非常完整、简洁和有条理。我留下了一些链接作为参考,这样你就会知道去哪里找。

干杯!


归档时间:

查看次数:

1006 次

最近记录:

5 年,9 月 前