I am creating simple news app, using Nuxt.js and Firebase Cloud Firestore, I easily retrieve all document data from a database, The problem is, I want to get a certain amount of data, exactly I want to retrive only 4 document from category politics
collection,
import { db } from "/plugins/firebase.js"; export const state = () => ({ politicNews:[] }) export const mutations = { politicNewsInfo(state, payload){ state.politicNews.push(payload) } } export const actions = { async getAllpoliticInfo({commit}){ /* get all data from politics collection*/ const snapshot = await db.collection("politics").get(); return snapshot.docs.map((doc) => commit("politicNewsInfo", { news: doc.data().News, newsId: doc.id }) ); } } export const getters = { politicinfoResults(state){ return state.politicNews } }
This is what my Vuex store module looks like, I will get any advice, or documentary source, Thanks
To retrieve only a limited number of documents, you need two things:
db.collection("politics").orderBy(firebase.firestore.FieldPath.documentId()).limit(4).get()
Also see the documentation on ordering and limiting data .
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.