1

This is a problem with either promises or finding a different way to solve the problem.

I have a user, that makes a document about their capabilities e.g.

price: Bio:

called (contractorPage)

And i have users that look at these.

How do i make it so the users can save these by reference?

using firestore, can you make it so that you add these documents to a collection, but when he original document is changed these are also changed?

my first method is to save the Id of the contractorPage. Which is the user ID (UID) of the firebase auth of the person that made it.

Then To map through these to get all of the saved "id's" documents.

  const [ idList, setIdList ] = useState([
  

    ])
    const [ contractorList, setContractorList ] = useState([
    ])



useEffect(()=>{

/// IdList has the list of id's
//me.uid is the firebase auth user ID
firestore.collection("SavedId's").doc(me.uid).collection("id").get()
.then( async (querySnapshot) => {
  await  querySnapshot.forEach(doc => {
        console.log(doc.id, " => ", doc.data());
        setIdList([...idList, doc.data()]);
        console.log("first",idList)
    });
    console.log("second",idList)
// contractorList has the list 
idList.map((data, index) => (
firestore.collection("contractorPages").doc(data.id).get().then((word)=>{
    console.log("hi", word.data())
    if (word.exists) {
        setContractorList([...contractorList, word.data()])
    }
})
)

);
}) 

},[])

This does not work, because the idList.map() function runs before setIdList([...idList, doc.data()]); has finished.

How do I make sure the idList is set before I try to retrieve data from firestore with what is in it?

Alexander Hemming
  • 753
  • 1
  • 6
  • 28
  • Hi there!, have you tried adding a `.then` after the `.then( async (querySnapshot) => {` in order to force the `idList.map()` block to run after the `setIdList`. Data consistency or what you call `can you make it so that you add these documents to a collection, but when he original document is changed these are also changed?` is addressed in this firebase video: https://www.youtube.com/watch?v=i1n9Kw3AORw. Have you taken a look at it? – Antonio Ramirez Mar 11 '21 at 21:05
  • yes I had tried that the problem was that i was setting the state. the state setting is on a different event loop so the solution was to just only set the state after it is all finished. – Alexander Hemming Mar 11 '21 at 21:37
  • Since SO is forum of questions and answers, can you include the working code as an answer so that it may help others? – Antonio Ramirez Mar 11 '21 at 23:43
  • ill just delete it as the question does not help the answer, – Alexander Hemming Mar 13 '21 at 16:23
  • the answer is this if you want to swap it round https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately – Alexander Hemming Mar 13 '21 at 16:23
  • Thank you for that Alex. I have flagged this question as duplicate so that others are redirected to the solution you have used. – Antonio Ramirez Mar 17 '21 at 23:00
  • Does this answer your question? [useState set method not reflecting change immediately](https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately) – Antonio Ramirez Mar 17 '21 at 23:00

0 Answers0