6

// 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,
        });
      }
    })
  })

firebase

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Abdullah Ch
  • 1,678
  • 1
  • 13
  • 31

1 Answers1

4

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193