简体   繁体   中英

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()
     }))

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM