繁体   English   中英

无法从 Firestore React-Native 获取所有数据

[英]Can't fetch all data from Firestore React-Native

我无法在我的 React 本机应用程序中从 Firestore 获取所有数据。 我可以在添加 limit(200) 参数时获取数据,但是当我删除 limit 参数时,我的应用程序崩溃了。 我需要显示所有数据,但我不知道自己做错了什么,这是我的代码:

  const listingsRef = firebase.firestore().collection(ServerConfiguration.database.collection.LISTINGS);

  if (categoryId === '') {
    console.log('Kommer vi hit???');

    return listingsRef
      .where('isApproved', '==', isApproved)
      .limit(2000)
      .onSnapshot((querySnapshot) => {
        const data = [];

        if (querySnapshot !== undefined) {
          querySnapshot.forEach((doc) => {
            const listing = doc.data();
            if (favorites && favorites[doc.id] === true) {
              listing.saved = true;
            }
            data.push({ ...listing, id: doc.id });
          });

          console.log('KOMMERR datan? : ', data);

          callback(data);
        }
      });
  }

我尝试使用 get 而不是快照,我得到了相同的结果。 我卡了好几天了!

你可以用获取和获取满足你条件的所有记录来代替限制。

const listingsRef = firebase.firestore().collection(ServerConfiguration.database.collection.LISTINGS);

  if (categoryId === '') {
    console.log('Kommer vi hit???');

    return listingsRef
      .where('isApproved', '==', isApproved)
      .get()
      .onSnapshot((querySnapshot) => {
        const data = [];

        if (querySnapshot !== undefined) {
          querySnapshot.forEach((doc) => {
            const listing = doc.data();
            if (favorites && favorites[doc.id] === true) {
              listing.saved = true;
            }
            data.push({ ...listing, id: doc.id });
          });

          console.log('KOMMERR datan? : ', data);

          callback(data);
        }
      });
  }

但是,当您有大量数据时,对结果进行分页总是一个好主意

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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