如标题所示,当调用以下函数时,我将得到这个问题
const CustomAgenda = () => {
return <ScrollView>{returnArray()}</ScrollView>;
};它正在调用returnArray函数
const returnArray = async () => {
let set = [];
console.log(6);
await getJobs().then(() => {
console.log(7);
set = j.map(x => {
return (
<View>
<Text> {x.itemType} </Text>
</View>
);
});
});
await console.log(set);
};最后,这个控制台日志如下所示--这是应该的,但我仍然得到相同的错误
[<View><Text> Carerre </Text></View>, <View><Text> Car </Text></View>, <View><Text> Car </Text></View>, <View><Text> Car </Text></View>, <View><Text> Car </Text></View>]我相信错误可能出现在下面两个函数中。
下面的函数从数据库中获取作业UID,然后调用getJobData函数来返回每个作业的信息
async function getJobs() {
console.log(8);
await firestore()
.collection('Users')
.doc(global.uid)
.get()
.then(async documentSnapshot => {
console.log(9);
console.log(10);
await getJobData(documentSnapshot.get('jobs'));
});
}getJobData函数将返回的数据推送到名为“j”的全局数组中。
const getJobData = async jobList => {
for (const i of jobList) {
let item = {};
await firestore()
.collection('Jobs')
.doc(i)
.get()
.then(documentSnapshot => {
item.id = i;
item.itemType = documentSnapshot.get('itemType');
item.size = documentSnapshot.get('size');
item.type = 'Pickup';
})
.then(() => {
j.push(item);
});
}
};'j‘数组返回以下内容--这也是正确的。
[{"id": "fST5A2WzgUR66xxNdHAU", "itemType": "Carerre", "size": "Large", "type": "Pickup"}, {"id": "cKQLGqqtyyuxhO7ZuFLR", "itemType": "Car", "size": "Large", "type": "Pickup"}, {"id": "l3KCfGSKh3MCySje832f", "itemType": "Car", "size": "Large", "type": "Pickup"}, {"id": "B8byNmHdc6g5YGqTPovX", "itemType": "Car", "size": "Large", "type": "Pickup"}, {"id": "FQraUtV7uTDyvkux1Vkf", "itemType": "Car", "size": "Large", "type": "Pickup"}]在日志编号之后,它似乎是对getJobs的调用,因为日志按以下顺序发生-6-错误-8 9 10 7
谢谢你的帮助
发布于 2022-10-08 22:32:40
您需要在set中返回returnArray。但不管怎么说,这都无关紧要。您还需要以状态(数据,而不是组件)的形式存储结果。你正试图做出一个承诺,这是不起作用的--这是错误的最终原因。您需要等待数据,然后将其存储在状态中,这将触发呈现。
这样就没有必要使用returnArray了。
试试这个:
const CustomAgenda = () => {
const [data, setData] = useState([])
useEffect(() => {
getJobs().then(data => setData(data))
}, [])
return <ScrollView>{
data.map(x=> (
<View>
<Text> {x.itemType} </Text>
</View>
)}
</ScrollView>;
};https://stackoverflow.com/questions/74000940
复制相似问题