[英]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);
}
})
})
}
I want to update my msg
field as well as content
field in contract
object, which in turn is present in contractDetails
object in the contract
array(0th index).我想更新我的msg
字段以及contract
object 中的content
字段,该字段又出现在contract
数组(第 0 个索引)的contractDetails
object 中。 I have searched and reached to my desired array value using onSnapShot
, how can I update these fields in the onSnapShot
method?我已经使用onSnapShot
搜索并达到了我想要的数组值,如何在onSnapShot
方法中更新这些字段? or I should use another approach for searching and updating fields in objects contained by the array.或者我应该使用另一种方法来搜索和更新数组包含的对象中的字段。
JUST A THOUGHT: If I could get the reference to the array index and then use it to update the object, maybe It'll work只是一个想法:如果我可以获得对数组索引的引用,然后使用它来更新 object,也许它会起作用
for example (I'll update sentForRevision
)例如(我将更新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,
});
}
})
})
There is no way you can query a Firestore collection based on a value that exists in an object that is contained in an array.您无法根据数组中包含的 object 中存在的值查询 Firestore 集合。 This kind of filtering cannot be achieved using partial data . 使用部分数据无法实现这种过滤。 I have even written an article regarding this topic called:我什至写了一篇关于这个主题的文章,叫做:
If you need that, you can duplicate the data on which you want to perform the filtering and add it to a separate array.如果需要,您可以复制要对其执行过滤的数据并将其添加到单独的数组中。 In this way, you can query the collection using array-contains
operator.这样,您就可以使用array-contains
运算符查询集合。 Once you get the desired documents, you can get the array, perform the updates and then write the documents back to Firestore.一旦获得所需的文档,就可以获取数组,执行更新,然后将文档写回 Firestore。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.