0

I am fetching some docs from firebase and then want to copy the docs to another array. But it copies the empty array before firebase finish updating the first array.

    const [docs, setDocs] = useState()
    const [docsCopy, setDocsCopy] = useState()

    db.collection('docs').get()
        .then(async snap=>{

            setDocs(snap.data().map(doc=>{
                return doc.name
            }))

            console.log(docs)

            setDocsCopy(docs)
        })

console.log also executing before setDocs().

Plz suggest a solution using async and await

FishLegs
  • 89
  • 10
  • 1
    The problem here is not with the api call but with the setDocs function. setDocs function is an async function. you can get the docs in a variable first and then pass it in setDocs(documents) and setDocsCopy(documents); – Rohan Veer Feb 27 '20 at 08:41
  • Thx man. It helped but still i want to know how to hold execution of a line untill previous line finishes its loop – FishLegs Feb 27 '20 at 08:43

0 Answers0