I am creating a new document in the firestore using set
function. I want to use the document data after the set function pushes data into the database.
// Add each record to their respective heads
let docRef = headCollectionRef.doc(data.headSlug).collection(recordsCollection)
.doc(String(data.recordData.sNo))
docRef.set(data).then(docSnap => {
agreementData.push(
docSnap.get().then(data => {
return data
})
)
})
Nothing written in the then()
is being executed.
Any help is much appreciated
set
returns Promise<void>
, so you don't have access to the doc data inside then
.
This is a situation where async/await will make your code much easier to read.
async function cool() {
const docRef = headCollectionRef.doc(...)
await docRef.set(data)
const docSnap = await docRef.get()
agreementData.push( docSnap.data() )
}
https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#set
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With