![](/img/trans.png)
[英]How to update an "array of objects" with Firestore and Firebase functions?
[英]How to search for specific fields in an array of objects containing objects in a document and update them in firestore (firebase) (JS)
// Update the other user that current user has asked for revision
export async function updateOtherUserForContractRevision(contractID : string, comments : any) {
// getting userDetails
let currentUser : string | any = localStorage.getItem("userDetails");
currentUser = JSON.parse(currentUser);
// update fields in contractRecieved
const contractsRecievedRef : any = doc(db, "contractsRecieved", currentUser.uid);
// const queryContractsRecieved = query(contractsRecievedRef,
// where('contractDetails.contract.contractID','array-contains',contractID)
// );
// console.log(".////////updateOtherUserForContractRevision", queryContractsRecieved ,contractID)
onSnapshot(contractsRecievedRef, (doc: any) => {
doc.data().contract.map((contract: any) => {
if(contract.contractDetails.contract.contractID === contractID) {
// I reach my desired array Index and want to update the msg field to my comments parametre
console.log(".////////contract", contract);
}
})
})
}
我想更新我的msg
字段以及contract
object 中的content
字段,该字段又出现在contract
数组(第 0 个索引)的contractDetails
object 中。 我已经使用onSnapShot
搜索并达到了我想要的数组值,如何在onSnapShot
方法中更新这些字段? 或者我应该使用另一种方法来搜索和更新数组包含的对象中的字段。
只是一个想法:如果我可以获得对数组索引的引用,然后使用它来更新 object,也许它会起作用
例如(我将更新sentForRevision
)
onSnapshot(contractsRecievedRef, async (doc: any) => {
doc.data().contract.map(async (contract: any) => {
if(contract.contractDetails.contract.contractID === contractID) {
console.log(".////////contract", doc.ref);
// If this contract.ref exists and points to the index present in the document
await updateDoc(contract.ref,{
"contractDetails.sentForRevision": true,
});
}
})
})
您无法根据数组中包含的 object 中存在的值查询 Firestore 集合。 使用部分数据无法实现这种过滤。 我什至写了一篇关于这个主题的文章,叫做:
如果需要,您可以复制要对其执行过滤的数据并将其添加到单独的数组中。 这样,您就可以使用array-contains
运算符查询集合。 一旦获得所需的文档,就可以获取数组,执行更新,然后将文档写回 Firestore。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.