首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >云功能更新子-集合

云功能更新子-集合
EN

Stack Overflow用户
提问于 2019-09-10 14:51:07
回答 2查看 1.1K关注 0票数 3

我正在尝试创建一个云功能,以便每当我的项目上的产品更新时触发它。我的想法是。

我有两个收藏,商店产品

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

为了使其工作,我需要创建一个云函数,以便在每次产品修改和查询所有具有相同产品id的存储并更新数据时触发云功能。

我在这件事上很难过。有人能在这里为我亮一盏灯吗?这是我的云功能。

代码语言:javascript
运行
复制
// 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“的值不是有效的查询约束。不能使用“未定义”作为Fi还原值。在(/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:273:15) at validateQueryValue (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:1844:18) at Query.where (/srv/node_modules/@google-cloud/firestore/build/src/reference.js:956:9) at exports.onProductChange.functions.firestore.document.onUpdate (/srv/lib/product-update.js:29:10)(/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at / cloudFunction /worker.js:825:24 at process._tickDomainCallback (cloudFunction/cloudFunction/next_tick.js:229:7)

EN

回答 2

Stack Overflow用户

发布于 2019-09-10 15:15:51

我建议你看看这个文档,特别是事件触发器

如果这有帮助的话请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2019-09-10 16:42:39

我认为snapshotsInStoreSubcollection.product是没有定义的

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

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

您不能在防火墙中将undefined设置为值,并且您正在尝试

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57873360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档