'Couldn't return data after findByCreate by setDoc (React JS + Firebase)

I'm implementing findOrCreate to return data created by setDoc() in Firestore + React JS. But it returns undefined.

Promise[[Prototype]]: Promise[[PromiseState]]: "fulfilled"[[PromiseResult]]: undefined

...
async findOrCreate(name: string, email: string, profileURL: string, userId: string) {
  const doc = await getDoc(this.doc(userId))
  const data = doc.data()
  if (data !== undefined) return
  const newData = await setDoc(this.doc(userId), {
    name, email, profileURL
  }).then((data) => {
    console.log('data', data)
    return data
  })
  console.log('newData', newData)
}
...


Solution 1:[1]

The setDoc() function just returns a Promise<void> and not document data. You can just console.log(data) as that's the data you are setting in document.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharmaraj