React native 如何在React Native中显示firebase firestore中的数据
就我的一生而言,我无法用它来呈现我从数据库中提取的数据 这是密码React native 如何在React Native中显示firebase firestore中的数据,react-native,React Native,就我的一生而言,我无法用它来呈现我从数据库中提取的数据 这是密码 function assetList(){ const [assetL, setAssetL] = useState([]); const [errors, setErrors] = useState(""); const getAssets = async () =>{ try{ const list = []; console.log("Brea
function assetList(){
const [assetL, setAssetL] = useState([]);
const [errors, setErrors] = useState("");
const getAssets = async () =>{
try{
const list = [];
console.log("Break");
db.collection("assets").get().then(function(querySnapshot){
querySnapshot.forEach(function(doc){
list.push(doc.data());
});
});
//problem
setAssetL([...list]);
//problem
console.log("list");
console.log(list);
console.log("AssetL");
console.log
} catch (e) {
setErrors("Failed To Load Data");
}
};
useEffect(() => {
getAssets();
}, []);
console.log(assetL);
return(
<SafeAreaView style = {styles.Heading}>
<View style = {styles.Heading}>
<Text style = {styles.headText}>Asset List</Text>
</View>
<FlatList
data = {assetL}
renderItem={({item}) => <Text>{item.default}</Text>}
/>
</SafeAreaView>
);
}
函数资产列表(){
const[assetL,setAssetL]=useState([]);
const[errors,setErrors]=useState(“”);
const getAssets=async()=>{
试一试{
常量列表=[];
控制台日志(“中断”);
db.collection(“assets”).get().then(函数(querySnapshot){
querySnapshot.forEach(函数(doc){
list.push(doc.data());
});
});
//问题
setAssetL([…列表]);
//问题
控制台日志(“列表”);
控制台日志(列表);
控制台日志(“AssetL”);
console.log
}捕获(e){
设置错误(“加载数据失败”);
}
};
useffect(()=>{
获取资产();
}, []);
控制台日志(assetL);
返回(
资产清单
{item.default}
/>
);
}
我已经缩小到至少最紧迫的问题,除了我把这个应用程序页面放在一起的jank方式之外,是setAssetL并没有实际设置assetL常量。有人能解释为什么会发生这种情况以及如何修复它吗?对于
getAssets
函数,请执行以下操作:
const getAssets = async () =>{
try {
const list = [];
console.log("Break");
db.collection("assets").get().then(function(querySnapshot){
querySnapshot.forEach(function(doc){
list.push(doc.data());
});
setAssetL(list);
});
...
} catch (e) {
setErrors("Failed To Load Data");
}
};
您的代码无法工作,因为db.collection(“assets”).get()函数返回一个承诺,您可以异步处理它,同时希望它是同步的
您可以阅读有关异步函数的更多信息谢谢,它很有效。然而,它仍然不会呈现在页面上。我想这是一个新问题。。。