0
votes

I cant pass the uid from the newly created user that is created from a firebase cloud function. The user is created successfully but the document in collection of the firestore is not. When i remove the argument from .doc() like this the user is created but with the random generated uid not the one created from the create user ofc. What i would like is to pass the uid from the created user to as the doc name. The uid is available from the response of the createUsr function.

I think i'm not doing something right with the way i'm using the async function.

const handleSubmit = async (values) => {
    const createUsr = functions.httpsCallable('createUsr')

    try {
        await createUsr({ email: values.email }).then((data) => {
            const id = data.uid
            const newDocRef = db.collection('students').doc(id)
            newDocRef.set({
              userID: id,
              firstName: values.firstName,
              lastName: values.lastName,
              access_type:  "student"
            })
        })
        success()
    } catch {
        error()
    }
  }

Data response from the console

Object
  data:
    disabled: false
    displayName: null
    email: "[email protected]"
    emailVerified: false
    metadata: {creationTime: "Tue, 06 Apr 2021 19:06:19 GMT", lastSignInTime: null, lastRefreshTime: null}
    passwordHash: null
    passwordSalt: null
    phoneNumber: null
    photoURL: null
    providerData: []
    tenantId: null
    tokensValidAfterTime: "Tue, 06 Apr 2021 19:06:19 GMT"
    uid: "GHpc2ykVj3RPnMqPDDiDiViVvmc2"
  __proto__: Object
  __proto__: Object
1
Try removing the await. - Ktoxcon
@KToxcon It now works but it creates the user from random generated uid like the data.uid was never passed - Filip Simin
Can you log data in the console and share it to me - Ktoxcon
@KToxcon the question has been updated with the response from the concole - Filip Simin

1 Answers

1
votes

Try with this:

const handleSubmit = async (values) => {
   const createUsr = functions.httpsCallable('createUsr')

   try {
    createUsr({ email: values.email }).then(({ data }) => {
        const { uid } = data;
        const newDocRef = db.collection('students').doc(uid);

        await newDocRef.set({
          userID: uid,          
          access_type:  "student",
          ...values
        });
    });

    success();
  } catch {
    error()
  }
}