Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase V8 to V9 conversion

I have this code snippet here that I want to convert from Firebase V8 to V9, but I can’t seem to find the correct syntax. Thanks for anyone’s help!

const [orders, setOrders] = useState()

db
 .collection("users")
 .doc(user?id)
 .collection("orders")
 .orderBy("created", "desc")
 .onSnapshot(snapshot => (
    setOrders(snapshot.docs.map(doc => ({
        id: doc.id,
        data: doc.data()
     }))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The onSnapshot() is a top level function Modular SDK and so are other methods used in this code snippet. Try refactoring the code as shown below:

const colRef = collection(db, `users/${user?.uid}/orders`);
const q = query(colRef, orderBy('createdAt', 'desc'));

onSnapshot(q, (querySnapshot) => {
  // ...
})

Checkout the documentation for more information.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading