0

我有一个名为“广告”的集合

/adverts/{advert_id} <-- 其中 advert_id 由 firestore 自动生成。

我有我的集合“用户”/user/{user_id} <--- 其中 user_id 由用户名定义

所以在“广告”文档中,我有下一张地图

user_data:{
    avatar_url: "",
    first_name: "example name",
    last_name: "example last name",
    rating: 5,
    username: "exampleusername"
}

每次创建广告时,此信息都来自用户文档。所以我想在每次用户更新他的数据时更新广告集合中的这张地图。

假设广告中可能存在多个文档,是否可以批量更新此字段?(我试图避免读取所有文件并重写它们,我只想写)

我试图通过(是一个 onUpdate 云函数)来实现这一点:

const before = change.before.data(); // Data before the update
const after = change.after.data(); // Data after the update
const user_id = after.username;

let batch  = db.batch()
let advertsRef = db.collection("adverts").where("user_data.username", "==", user_id)

batch.update(advertsRef, {
    "user_data.avatar_url": after.avatar_url,
    "user_data.first_name": after.first_name,
    "user_data.last_name": after.last_name,
    "user_data.overall_adverts_rating": after.overall_adverts_rating,
    "user_data.username": after.username,
 })

batch.commit().then(()=>{
  console.log("done")
})
.catch(error =>{
  console.log(error)
})

但我收到下一个错误:

Error: Value for argument "documentRef" is not a valid DocumentReference.
at Object.validateDocumentReference (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:2034:15)
at WriteBatch.update (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:312:21)
at /workspace/index.js:147:9
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:134:23)
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
at processTicksAndRejections (internal/process/task_queues.js:97:5) 

我猜是因为我的 .where() 没有引用特定文件。

4

1 回答 1

1

在您的云功能中,您需要遍历每个匹配的广告。即,您正在重写与查询匹配的所有文档,这意味着您需要阅读每个文档并更新每个文档。例如

let adverts = await db.collection("adverts").where("user_data.username", "==", user_id).get
for (doc of adverts.docs) {
  doc.user_data = after
  batch.update(doc)
}
await batch.commit()
于 2021-04-14T00:16:08.283 回答