简体   繁体   English

如何在包含文档中对象的对象数组中搜索特定字段并在 firestore (firebase) (JS) 中更新它们

[英]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.

相关问题 如何使用 Firestore 和 Firebase 函数更新“对象数组”? - How to update an "array of objects" with Firestore and Firebase functions? Firebase Firestore,如何有效地更新对象数组中的多个集合 - Firebase Firestore, How to update multiple collection from array of objects efficiently Firestore 集合到具有 Firebase v9 的对象数组 - Firestore collection to array of objects with Firebase v9 如何使用 Kotlin 更新 Firestore 中对象数组中的数据? - How to update data in an array of objects in Firestore using Kotlin? 如何在 Android 上使用 whereArrayContains() 过滤器查询包含 Firestore 集合中对象数组的文档? - How to query documents containing array of objects in Firestore collection using whereArrayContains() filter on Android? Firebase Firestore - 如何从特定文档中检索数组字段并将这些数组值用于 map 文档 - Firebase Firestore - how to retrieve an array field from a specific document and use those array values to map documents 如何更新 Firestore 文档中特定字段的值? - How to update value of a specific field in Firestore document? Cloud Firestore:使用动态键更新嵌套对象中的字段 - Cloud Firestore: Update fields in nested objects with dynamic key 将 Firestore 文档转换为对象列表 - Convert Firestore document to list of objects 使用 admin sdk 和 Firebase 云函数从 Firestore 检索对象数组 - Retrieving an array of objects from Firestore with the admin sdk and Firebase cloud functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM