[英]Can't fetch all data from Firestore React-Native
I can't fetch all data from Firestore in my react native app.我无法在我的 React 本机应用程序中从 Firestore 获取所有数据。 I can fetch data when I add a limit(200) parameter but when I take away the limit parameter my app crashes.
我可以在添加 limit(200) 参数时获取数据,但是当我删除 limit 参数时,我的应用程序崩溃了。 I need to display all the data and I don't know what I'm doing wrong, here is my code:
我需要显示所有数据,但我不知道自己做错了什么,这是我的代码:
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);
}
});
}
I have tried to use get instead of on Snapshot, and I get the same results.我尝试使用 get 而不是快照,我得到了相同的结果。 I have been stuck for days!
我卡了好几天了!
You can replace the limit with getting and getting all the records that satisfy your condition.你可以用获取和获取满足你条件的所有记录来代替限制。
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);
}
});
}
But its always a good idea to paginate your results when you have large amount of data但是,当您有大量数据时,对结果进行分页总是一个好主意
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.