【发布时间】:2025-12-10 04:50:01
【问题描述】:
这是我试图在这里完成的基本前提。如果用户询问有关产品的问题,我想向当前拥有该产品的其他用户发送通知。基本上是说“嘿,某某对这个产品有疑问。也许你可以帮忙,因为你已经拥有它了”
每个 userProfile 集合都有一个名为“notify”的子集合,其中存储了各种事物的通知。我需要做的是对 userProducts 进行排序并找到拥有该产品的每个用户,然后仅在拥有该产品的特定用户的通知子集合中创建一个通知帖子。
这里是基本代码。第一点起作用,因为它确实返回了拥有该产品的用户 ID 数组。我现在苦苦挣扎的地方是让它在 Notify 子集合中为那些特定用户创建一个新文档。这可能吗?
exports.Questions = functions.firestore
.document("/userPost/{id}")
.onCreate(async (snap, context) => {
const data = snap.data();
if (data.question == true) {
const userProducts = await db
.collection("userProducts")
.where("product", "==", data.tag)
.get();
const userData = userProducts.docs.map((doc) => doc.data().userId);
await db
.collection("userProfile")
.where("userId", "in", userData)
.get()
.then((querySnapshot) => {
return querySnapshot.docs.ref.collection("notify").add({
message: "a user has asked about a product you own",
});
});
});
【问题讨论】:
标签: javascript firebase google-cloud-firestore google-cloud-functions