skip to Main Content

I want to add a new map in array named players but everytime i try to add it with this code

            await setDoc(
                doc(firestore, 'gambles', gambleId),
                {
                    players: arrayUnion({
                        balanceDrop: silver,
                        id: userId,
                        login: userData.login
                    }),
                    totalPlayers: increment(1),
                    totalSilver: Number(gambleData?.totalSilver) + Number(silver)
                },
                { merge: true }
            );

and there is already a map with same values another map is not being created only totalSilver and totalPlayers values are being updated I just want another map even if values are the same
enter image description here

2

Answers


  1. try the following.

    import { v4 as uuidv4 } from 'uuid';
    
    const playerId = uuidv4();
    
    await setDoc(
      doc(firestore, 'gambles', gambleId),
      {
        players: arrayUnion({
          id: playerId,
          balanceDrop: silver,
          login: userData.login
        }),
        totalPlayers: increment(1),
        totalSilver: Number(gambleData?.totalSilver) + Number(silver)
      },
      { merge: true }
    );
    

    let me know any?

    Login or Signup to reply.
  2. arrayUnion is behaving exactly as it’s supposed to here. It will not add new array elements that are the same as any existing elements. That’s why it’s called a "union" operation as opposed to a "push" operation. This is made clear in the documentation:

    arrayUnion() adds elements to an array but only elements not already present.

    If you want to add a duplicate element, you have to first read the document, modify the array in memory to contain whatever duplicates you want, then update the array field back to the document.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search