warm__tape
warm__tape

Reputation: 80

Problem creating document within subcollection

I'm working on a signup form. User registers, then in Firebase in the "companies" collection I add a new company, then a "users" collection is added under the just-created company, and the just-created user is added as a document to the company.

Everything is working fine until my code tries to add a new collection "users" under the just created company. Here's my code thus far:

return async (dispatch, getState) => {
    try {
        const { companyName, email, password } = getState().user;
        const response = await Firebase.auth().createUserWithEmailAndPassword(
            email,
            password
        );
        if (response.user.uid) {

            // Set up company
            const company = {
                name: companyName,
                owner: response.user.uid
            };

            // Create company
            db.collection("companies")
                .add(company)
                .then(ref => {

                    console.log("Company ID ", ref.id);
                    console.log("User ID ", response.user.uid);

                    // Set up first user
                    const user = {
                        uid: response.user.uid,
                        email: email
                    };

                    // Add first User
                    db.doc(ref.id)
                        .collection("users")
                        .add(user);

                });

            dispatch({ type: SIGNUP, payload: user });
        }
    } catch (e) {
        alert(e);
    }
};

Those console.logs return the correct IDs, So it looks like there's an issue with how my reference to the just-created company is being created? How would I fix that?

Upvotes: 0

Views: 39

Answers (2)

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

In addition to zkohi's correct answer, note that you are mixing the use of async/await with then(), which is not recommended. The following should do the trick (untested):

  return async (dispatch, getState) => {
    try {
      const { companyName, email, password } = getState().user;
      const response = await Firebase.auth().createUserWithEmailAndPassword(
        email,
        password
      );
      if (response.user.uid) {
        // Set up company
        const company = {
          name: companyName,
          owner: response.user.uid,
        };

        // Create company
        const compRef = await db.collection('companies').add(company);

        console.log('Company ID ', compRef.id);

        const user = {
          uid: response.user.uid,
          email: email,
        };

        await compRef.collection('users').add(user);

        dispatch({ type: SIGNUP, payload: user });
      }
    } catch (e) {
      alert(e);
    }
  };

Note how we define the users subcollection:

compRef.collection('users').add(user);

just by calling the collection() method on the company's DocumentReference. This last point is in line with zkohi's answer.

Upvotes: 1

zkohi
zkohi

Reputation: 2520

db.doc(ref.id).collection("users").add(user) may be wrong.

Could you try ref.collection("users").add(user) or db.collection("companies").doc(ref.id).collection("users").add(user);

                    // Add first User
                    // db.doc(ref.id)
                    //    .collection("users")
                    //    .add(user);
                    ref.collection("users").add(user);
                    // db.collection("companies").doc(ref.id).collection("users").add(user);

See:

Upvotes: 2

Related Questions