【发布时间】:2020-01-12 08:42:24
【问题描述】:
我正在尝试创建一个云功能,以便在我的项目中的产品每次更新时触发。这是想法。
我有 2 个系列,商店和产品。
在 stores 集合中,有一个名为 products 的子集合,其中包含商店销售的所有产品。通过从主 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”的值不是有效的查询约束。不能将“未定义”用作 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 在process._tickDomainCallback (internal/process/next_tick.js:229:7)
【问题讨论】:
-
究竟是什么不工作?
-
onWrite 可能是首选,如果用户可以添加新产品并且它们也需要被复制。此外,您应该返回您的 batch.commit()。需要有关错误的更多信息。另外,它在你的“功能”日志中说了什么?
-
大家好,对不起。我已经添加了错误日志。
标签: firebase google-cloud-firestore google-cloud-functions