我正在尝试创建一个云功能,以便每当我的项目上的产品更新时触发它。我的想法是。
我有两个收藏,商店和产品。
在商店集合中,有一个名为 products 的子集合,它包含商店销售的所有产品。数据通过复制主产品根集合中的特定项目来“得到”,其思想是我的项目具有良好的性能和成本效益。
为了使其工作,我需要创建一个云函数,以便在每次产品修改和查询所有具有相同产品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“的值不是有效的查询约束。不能使用“未定义”作为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)
https://stackoverflow.com/questions/57873360
复制相似问题