【发布时间】:2021-02-26 10:55:33
【问题描述】:
我只是想用我的状态 (ListOfProducts) 渲染我的 Flalist,它是一个 FirestoreDocumentSnapshot 数组,如下所示:
但是没有渲染。这是我的代码:
return (
<View>
<TouchableOpacity onPress={() => console.log(listOfProducts)}>
<Text>Log ListOfProducts</Text>
</TouchableOpacity>
<View style={{flex: 1}}>
<FlatList
style={{flex: 1, width: 200, height: 200}}
data={listOfProducts}
keyExtractor={(item, index) => {
item[index].id;
}}
renderItem={({item}) => (
<View style={{flex: 1}}>
<Text style={{height: 100, width: 100, backgroundColor: 'red'}}>
MON CHIBROS
</Text>
</View>
)}
/>
</View>
</View>
);
像这样从 Firestore 获取我的数据:
useEffect(() => {
const unsubscribe = getUserLocation()
.then((location) => getProducts(location.coords))
.catch((e) => {
throw new Error(e.message);
});
const getProducts = async (coords) => {
console.log('getProducts');
let nearbyGeocollections = await geocollection
.limit(10)
.near({
center: new firestore.GeoPoint(coords.latitude, coords.longitude),
radius: 50,
})
.get();
let nearbyUsers = [];
await nearbyGeocollections.forEach((geo) => {
if (geo.id !== user.uid) {
firestore()
.collection('PRODUCTS')
.doc(geo.id)
.collection('USER_PRODUCTS')
.onSnapshot((product) => {
nearbyUsers.push(product.docs);
});
}
});
setLoading(false);
setListOfProducts(nearbyUsers);
};
return () => {
unsubscribe;
setListOfProducts([]);
};
}, []);
如果有人能指出我的错误,我将不胜感激。
【问题讨论】:
-
你是如何获取数据的,也许你忘记调用 .data()。还请提及您获取数据以获得答案的方式。
-
我正在从 Firestore 获取数据。我编辑了我的问题。
-
感谢您的回复,但我的 DocumentSnapshot 很好,这就是我想要的。在您的回答中,我从每个 DocumentSnapshot 中获取数据。我刚刚给出了答案,我必须重建一个新对象,它现在可以工作了。
标签: javascript react-native google-cloud-firestore react-native-flatlist