我有一个名为meterData的List变量,我想将列表项的值插入到React本机Elements组件ListItem.Accordion
中。这是我的代码,到目前为止,我得到的错误是Can't find variable : meterData
function getReadingOrder(Emp_Number,{navigation}){
fetch('http://somewebsite/gecol/partner/api.php?emp_number='+`${Emp_Number}`, {
method: 'POST',
headers: {
Accept: 'text/plain',
'Content-Type': 'text/plain'
}
})
.then((response) => response.text())
.then((data) => { var xml2js = require('xml2js');
var meterData = "";
var parser = new xml2js.Parser();
parser.parseString(data, function(err,result){
//Extract the value from the data element
const meterData = [{
servicePoint:result['counter']['serviceNumber'],
counternumber:result['counter']['counternumber'],
consumer:result['counter']['consumername'],
consumpationtype:result['counter']['consumpationtype'],
currentread:result['counter']['currentread'],
currentdate:result['counter']['currentdate'],
lastread:result['counter']['lastread'],
lastdate:result['counter']['lastdate'],
consumpation:result['counter']['consumpation'],
phonenumber:result['counter']['phonenumber']
}]
// getRounds(meterData);
});
});
const [expanded, setExpanded] = React.useState(true);
return (
<SafeAreaProvider >
<View style={ReaderHome.Accordion}>
{meterData.map((meter, i) => (
<ListItem.Accordion
content={
<>
<Avatar size={64} icon={{ name: 'home', type: 'AntDesign' }} containerStyle={{ backgroundColor: '#1f95c0' }}/>
<ListItem.Content>
<ListItem.Title style={{left:10}}><Text style={ReaderHome.ConsumerName}>meter.consumer</Text></ListItem.Title>
<ListItem.Subtitle style={{left:10}}><Text style={ReaderHome.ServiceNumber}>meter.servicePoint</Text></ListItem.Subtitle>
</ListItem.Content>
</>
}
isExpanded={expanded}
onPress={() => {
setExpanded(!expanded);
}}
>
<ListItem key={i} onPress={console.log('s')} bottomDivider>
<Avatar title={''} source={''} />
<ListItem.Content>
<ListItem.Title>{meter.currentread}</ListItem.Title>
<ListItem.Subtitle>{meter.lastread}</ListItem.Subtitle>
</ListItem.Content>
<ListItem.Chevron />
</ListItem>
</ListItem.Accordion>))}
</View>
</SafeAreaProvider>);
}
1条答案
按热度按时间lnlaulya1#
meterData
是在fetch的then作用域中声明的,所以以后不能从外部访问它。另外,由于fetch是异步的,所以在渲染时它也不可用。由于它是异步的,因此需要进行一些重构,请查看此post,了解如何在组件内使用钩子进行提取。