简体   繁体   中英

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). I have searched and reached to my desired array value using onSnapShot , how can I update these fields in the onSnapShot method? 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

for example (I'll update 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. 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. Once you get the desired documents, you can get the array, perform the updates and then write the documents back to Firestore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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