gpt4 book ai didi

javascript - 如何防止更改子节点字段后父节点触发云函数?

转载 作者:行者123 更新时间:2023-11-30 23:58:54 25 4
gpt4 key购买 nike

我的 Firebase 实时数据库的结构如下:

-- Stock
-- StoreId
-- DepartmentId_1
-- ProductId_1
-- name: "productName"
-- price: 1.0
-- DepartmentId_2
-- ProductId_X
-- name: "productNameX"
-- price: 1.0

我想读取 DepartmentId 以便在 RecyclerView 中显示它们。但 Android Firebase Listener 会检索 StoreId 中包含的所有数据。

我知道我可以使用 for 循环并获取 key ,但这不是重点。问题在于下载的数据量,这可能会超出我在 Firebase 中的配额。

因此,我决定使用 Cloud Functions 在其他节点中写入 key ,如下所示:

-- Departments
-- StoreId
-- DepartmentId_1: true
-- DepartmentId_2: true
-- DepartmentId_3: true
-- DepartmentId_4: true
.
.
.

我以为 Cloud Functions 只会在特定节点中触发,但事实证明,如果我更改 price 字段,我的 Cloud Function 就会被触发。

所以我的问题是:如何仅在添加或删除 departmentId 时触发我的云功能?

下面是我的第一步,那么如何循环遍历 departmentId 并将它们写入 Departments 节点中?

exports.createStoreDepartments = functions.database
.ref('Stock/{storeId}')
.onWrite((change,context) => {
console.log('DATA CHANGED');
return true;
});

感谢任何帮助!

最佳答案

How to trigger my Cloud Function only when either a departmentId is added or removed?

有两种方法可以做到这一点:

  1. 有两个独立的触发器。

    exports.createStoreDepartments = functions.database
    .ref('Stock/{storeId}/{departmentId}')
    .onCreate((snapshot, context) => {
    ...

    exports.createStoreDepartments = functions.database
    .ref('Stock/{storeId}/{departmentId}')
    .onDelete((snapshot, context) => {
    ...
  2. 使用单个 onWrite 触发器。

    exports.createStoreDepartments = functions.database
    .ref('Stock/{storeId}/{departmentId}')
    .onWrite((change,context) => {
    if (!change.before.exists()) {
    // TODO: this department was just created
    }
    if (!change.after.exists()) {
    // TODO: this department was just deleted
    }});

关于javascript - 如何防止更改子节点字段后父节点触发云函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60876926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com