// 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); } } Quiero agregar userContractData a la matriz presente en el documento ( pendingContract ) de la colección de contrato pendiente. Pero lo que hace mi código es que solo actualiza los datos ya presentes en el índice 0. ¿Cómo puedo estructurar mi userContractData junto con el método setDoc que puedo agregar a la matriz?
De hecho, está configurando el valor del primer elemento de la matriz en el documento. Intente usar arrayUnion() para insertar elementos en una matriz:
await setDoc(newPendingContractRef, { contract: arrayUnion(...userContractData) } , {merge: true});