Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore then() function not executing after set()

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

like image 656
kt14 Avatar asked Apr 24 '18 18:04

kt14


1 Answers

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

like image 181
JeffD23 Avatar answered Sep 27 '22 22:09

JeffD23