简体   繁体   中英

Firebase Cloud Firestore, How to receive only 4 document using javascript?

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:

  • You need to tell the database to put the documents in a specific order.
  • You then need to tell it to return only a certain number of documents.
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.

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