Home > Enterprise >  How to add a sub collection to a document in firestore using react native
How to add a sub collection to a document in firestore using react native

Time:04-19

Im new to react native and firestore so bare with me.

Im trying to create a document within a collection called groups (Which works). Once it has been created im getting the doc ID of the created document and then trying to create a sub collection in it called admins and then a doc with an id for the current user and then setting the isAdmin field to be true.

I dont know if i'm making aa mistake or the logic isnt even right.

Im getting an unhandle promise rejection error and an output for the console.logs shown below

Document retreived with ID: snPXEsZ8Bp7xQkJJtlj0
Current User ID: vhZFUdJWeJXjcQHCVzVE3Y9meU33

[Unhandled promise rejection: TypeError: _firebase.db.collection('groups').doc(docId).collection('admins').doc(userId).add is not a function. (In '_firebase.db.collection('groups').doc(docId).collection('admins').doc(userId).add({]

const submitGroup = async () => {
        await db
            .collection('groups')
            .add({
                userId: user.uid,
                groupName: groupName,
                groupImage: null,
                postTime: firebase.firestore.Timestamp.fromDate(new Date()),
                groupPrivacy: selectedItem,
                
            })
            .then((docRef) => {

              // get & pass the doc id created above to var 
                const docId = docRef.id;

              //pass the current user id to var
                const userId = user.uid;

                const addAddmins = async (docId, userId) => {

                    console.log('Document retreived with ID: ', docId);
                    console.log('Current User ID: ', userId);

                    await db.collection('groups')
                        .doc(docId)
                        .collection('admins')
                        .doc(userId)
                        .add({
                            isAdmin: true,
                        })
                        .then(() => {
                            //Alert success

                        }).catch((error) => {
                            console.log('Something went wrong', error)
                        })
                }
                //Call add sub collection and pass parent doc ID and current userID
                addAddmins(docId, userId)
           
            })
            .catch((error) => {
             
                console.log('Something went wrong', error)

            })


    }

CodePudding user response:

I have not tested this code and just check that the groupId has the key id in it.

const submitGroup = async () => {
  const userId = user.uid;

  const groupId = await db.collection("groups").add({
    userId: user.uid,
    groupName: groupName,
    groupImage: null,
    postTime: firebase.firestore.Timestamp.fromDate(new Date()),
    groupPrivacy: selectedItem
  });
  console.log("groupId:", groupId.id);
  
  const addAdmins = await db
    .collection("groups")
    .doc(groupId.id)
    .collection("admins")
    .doc(userId)
    .add({
      isAdmin: true
    });
};

CodePudding user response:

Okay I managed to figure out my problem i had to change the .add({}) to .set({}). I guess becuase the parent collection had already been made by the first query.

  • Related