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()
}))
CodePudding user response:
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.