繁体   English   中英

如何在 firestore v9 中查询和更新

[英]How to query and update in firestore v9

async booleanFunction(email: string, bool: boolean){
    console.log('here')
    const db = getFirestore();
    const q = query(collection(db, "users"), where("email", "==", email), limit(1));
    const querySnapshot = await getDocs(q);
    await updateDoc(querySnapshot, {
      disabled: !bool
    });
}

这是我的 function,它接受 email 和一个布尔值(这需要更新),我没有 firestore 提供的 _id,所以我需要一些替代方法来查询数据并更新它

这是显示的错误

Argument of type 'QuerySnapshot<DocumentData>' is not assignable to parameter of type 'DocumentReference<{ disabled: boolean; }>'.

类型“QuerySnapshot”缺少类型“DocumentReference<{ 禁用的以下属性:boolean; }>':转换器、类型、firestore、id 和另外 3 个。

问题在这里:

await updateDoc(querySnapshot, {
  disabled: !bool
});

您正在尝试在QuerySnapshot上调用updateDoc ,这在 v8 上是不可能的,而且也从未有过。

您只能在DocumentReference上调用updateDoc 因此,您必须遍历QuerySnapshot中的文档,并对每个文档调用updateDoc

await Promise.all(() => 
  querySnapshot.docs.map((doc) => 
    updateDoc(doc.ref, { disabled: !bool })
  )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM