0

I want to get user data from the return of creating a user, as below:

const newUserRes = await db.collection('users').add(userData);

Do you have any suggestions for me to get the new user document straight away from newUserRes?

I don't feel right to call a new read to get it:

const newUserRef = await db.collection('users').doc(newUserRes.id).get();

const newUser = newUserRef.data()
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Thanh Nhật
  • 777
  • 1
  • 7
  • 14

2 Answers2

0

The add() method returns an asynchronous DocumentReference, which does not contain a snapshot of the data that was just written. So you will indeed have to read the document after writing it, just a you do in your second snippet.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

I found the solution to getting new user data directly from the write return (newUserRes)

const newUserSnapshot = await newUserRes.get();

const newUserData = newUserSnapshot.data();
Thanh Nhật
  • 777
  • 1
  • 7
  • 14