0
// sending contracts to user
export async function sendContractToOtherUser(email: string | any, contract: string) {
  try {

    let user : any = "";
    // search the users collection with the email and get the user id

    // Create a reference to the users collection
    const usersRef =  collection(db, "users");
    // Create a query against the collection.
    onSnapshot(usersRef, async(snapShot) => {
  
      snapShot.docs.forEach(doc => {
        if(doc.data().email === email) {
         user = doc.data();
        }} 
      );  
      console.log("//////user Found", user, typeof user, user.userID);

      // An Array which Contains an object
      const userContractData : any = [{
        userID : user.userID,
        contract: contract
      }];
      // setting the array 
      const newPendingContractRef : any = doc(db,"pendingContract", user.userID );
       await setDoc(newPendingContractRef,
        {...userContractData}
        , {merge: true});
      // console.log("/////////contract", pendingContract);

    });

  } catch (error) {
      console.log("/////////errror in sendContractToOtherUser", error);
  }
}

I want to append userContractData to the array present in the document (user.uid) of pendingContract collection. But what my code does that it only updates the already present data at 0th index. How can I structure my userContractData along with the setDoc method that I can append into the array

enter image description here

1 Answer 1

3

You are indeed setting value of first array item to the document. Try using arrayUnion() to push elements to an array:

await setDoc(newPendingContractRef,
        { contract: arrayUnion(...userContractData) }
        , {merge: true});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.