Firebase 云函数更新子集合

Firebase 云函数更新子集合,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,我正在尝试创建一个云函数,以便在每次更新项目中的产品时触发。这是我的想法 我有两个系列,商店和产品 在商店集合中,有一个名为产品的子集合,其中包含商店销售的所有产品。数据通过从主产品根集合复制特定项来“输入” 我的想法是我的项目得到了良好的性能和成本效益 为了实现这一点,我需要创建一个云函数,在每次修改产品时触发,并查询所有具有相同产品id的商店,并更新数据 我真的很难受。有人能帮我照一盏灯吗?这是我的云功能 // Exporting the function export const onPr

我正在尝试创建一个云函数,以便在每次更新项目中的产品时触发。这是我的想法

我有两个系列,商店产品

商店集合中,有一个名为产品的子集合,其中包含商店销售的所有产品。数据通过从主产品根集合复制特定项来“输入” 我的想法是我的项目得到了良好的性能和成本效益

为了实现这一点,我需要创建一个云函数,在每次修改产品时触发,并查询所有具有相同产品id的商店,并更新数据

我真的很难受。有人能帮我照一盏灯吗?这是我的云功能

// Exporting the function
export const onProductChange = functions.firestore
.document('products/{productId}')
// Call the update method
.onUpdate(async (snap, context) => {
    // Get the product ID
    const productID = context.params.productID;
    // Query for all the collections with the specific product ID.
    const resultSnapshot = await db.collectionGroup('products')
        .where('id', '==', productID).get();

    // Filter for the collections with the 'products' root and return an array.
    const snaphotsInStoreSubcollection = resultSnapshot.docs.filter(
        (snapshot: any) => {
            return snapshot.ref.parent === 'products';
    });

    const batch = db.batch();
    // Takes each product and update
    snaphotsInStoreSubcollection.forEach((el: any) => {
        batch.set(el.ref, snaphotsInStoreSubcollection.product);
    });
    await batch.commit();
});

云函数控制台上出现错误

错误:参数“Value”的值不是有效的查询约束。不能将“未定义”用作Firestore值。在Object.validateUserInput(/srv/node_modules/@google cloud/firestore/build/src/serializer.js:273:15)在validateQueryValue(/srv/node_modules/@google cloud/firestore/build/src/reference.js:1844:18)在Query.where(/srv/node_modules/@google cloud/firestore/build/src/reference.js:956:9)在exports.onProductChange.functions.firestore.document.onUpdate(/srv/lib/product update.js:29:10)在cloudFunction(/srv/node\modules/firebase functions/lib/cloud functions.js:131:23)在/worker/worker.js:825:24 at at-at-process.\勾选域回调(内部/process/next\u勾选.js:229:7)


我建议你看看这个,特别是在


让我知道这是否有帮助。

我认为
快照sinstoresubcollection.product
未定义

batch.set(el.ref,snaphotsInStoreSubcollection.product)

快照是一个文档,其数据为
snapshot.data()


您不能在firestore中将
undefined
设置为一个值,并且您正在尝试

到底什么不起作用?如果用户可以添加新产品并且还需要复制,则最好使用onWrite。此外,还应返回批处理。提交()。需要更多关于错误的信息。还有,你的“功能”日志上写了什么?嘿,伙计们,很抱歉。我已经添加了错误日志。