Node.js 在firestore中通过云函数进行更改后从文档检索对象数组

Node.js 在firestore中通过云函数进行更改后从文档检索对象数组,node.js,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我有一个firestore文档,其中有一个名为items的字段,它是生成每个项目的对象数组。结构如下: - Document1 | document_status | items | item1 | | name | | item_status | item2

我有一个firestore文档,其中有一个名为items的字段,它是生成每个项目的对象数组。结构如下:

- Document1
              | document_status
              | items
                     | item1
                     |   | name
                     |   | item_status
                     | item2
                     |   | name
                     |   | item_status
现在我正在使用onUpdate()方法收听文档更新,该方法运行良好。例如:如果文档状态更改,那么我将在change.after.data()方法中获得更改。但是我需要通过change.after.ref获得对“items”字段的引用,这样我就可以查看每个项目并更改项目的状态

我只想通过**更改访问文档,而不是直接访问。因为我正在使用通配符检索文档引用**

我在这里看到了一个很接近的例子:但不幸的是,我无法尝试更深入地获取子项的引用

这就是我所尝试的:

exports.sampleFunction = functions.firestore
        .document(‘mycollection/{documentId}/subCollection/{mydocuments}’).onUpdate((change, context) => {
        const newData = change.after.data();
        const documentStatus = newData.document_status;
         const parentRef = change.after.ref.child("items");
          return parentRef.once('value').then((snapshot) => {
            snapshot.forEach((item) => {
              updates[item.item_status] = "some_value"
            });
            return parentRef.update(updates);
          });
        }
        return;
    });
我还尝试使用:

在参考父项子项(“项目”)之后的变更

和不同的组合。但我得到的错误如下:

TypeError: change.after.ref.child is not a function
    at exports.sample_function.functions.firestore.document.onUpdate (/user_code/index.js:31:46)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
    at /var/tmp/worker/worker.js:710:26
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
TypeError:change.after.ref.child不是函数
在exports.sample\u function.functions.firestore.document.onUpdate(/user\u code/index.js:31:46)
反对。(/user_code/node_modules/firebase functions/lib/cloud functions.js:112:27)
在下一个(本地)
at/user\u code/node\u modules/firebase functions/lib/cloud functions.js:28:71
at_uuwaiter(/user_code/node_modules/firebase functions/lib/cloud functions.js:24:12)
在cloudFunction(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:82:36)
at/var/tmp/worker/worker.js:710:26
在进程中。_tickDomainCallback(internal/process/next_tick.js:135:7)

请建议我如何检查所有项目,并将所有项目状态更新为某个值

我不知道您的意思:“我只想通过更改访问文档,而不是直接访问。因为我使用通配符检索文档引用”。你能修改一下你的问题,更好地解释这意味着什么吗?另外,我认为如果你展示了你正在使用的全部功能,而不仅仅是其中的几行,那就太好了。我们应该能够看到您定义函数的方式。您有另一个主要问题,您说您使用的是Firestore,但您的代码似乎使用的API看起来像是实时数据库访问。感谢您的回答@DougStevenson我已经用完整的函数更新了我的问题。通过“我只想通过更改而不是直接访问文档”,意味着我的路径中有一个通配符,因此我希望每当有人试图更改属于给定路径的任何文档时,我的函数都会触发。如果你能给我指出正确的方向,我真的很感激。我使用这个firebase文档()来了解如何获取文档的引用。但不幸的是,我发现很难获取子级的引用并获取其子级数组。使用Firestore API来处理文档和文档引用,而不是实时数据库API。