Reputation: 177
Hello I would like to convert the following firebase 8 code to firebase 9 and have no clue how to do it - any help would be welcome!
This piece of code should go inside the userDoc and then fetch the collection that is insdie that document and return all of the posts that belong to that user
if (userDoc) {
user = userDoc.data()
const postsQuery = userDoc.ref
.collection('posts')
.where('published', '==', true)
.orderBy('createdAt', 'desc')
.limit(5)
posts = (await postsQuery.get()).docs.map(postToJSON)
}
Upvotes: 2
Views: 542
Reputation: 50850
Firestore's documentation has examples for both V8 and V9 so you can easily compare them and modify your code. Just switch to modular
tab for the new syntax. The code in question would be like:
import { collection, query, where, getDocs, orderBy, limit } from "firebase/firestore";
const q = query(collection(db, "posts"), where("published", "==", true), orderBy("createdAt", "desc"), limit(5));
const querySnapshot = await getDocs(q);
posts = querySnapshot.docs.map(postToJSON)
Upvotes: 1