我是本机反应的新手。我正在尝试感染Web服务,并从响应中输入一些元素给Picker(我使用基于本机的数据库)。我的问题是我不知道如何LIBELLE
在Json中获取所需的元素(在这里是)。
看代码:
getSurfaces(){
fetch('yourWebservice')
.then((response) => response.text())
.then((responseText) => {
parseString(responseText, function (err, result) {
responseText = result;
});
console.log(JSON.stringify(responseText));
this.setState({
surfaces: responseText.Surfaces.surface
}).catch(err => console.log(err));
console.log(this.state.surfaces);
}
})
.catch((err) => {
console.log('Error fetching the feed: ', err)
})
}
componentDidMount(){
this.getSurfaces();
}
我的构造函数:
constructor (){
super();
this.state = {
date: '',
surface: '',
surfaces: [],
start: '',
hours : [],
};
this.valueChangeSurface = this.valueChangeSurface.bind(this);
}
valueChangeSurface(value: String){
this.setState({
surface: value
});
}
我的renderSurfaces方法:
renderSurface(){
if(this.state.surfaces){
return this.state.surfaces.map((surface) => {
return <Picker.Item label={surface.LIBELLE} value={surface}/>
})
}
}
Picker的呈现:
<ListItem>
<Left>
<Text>Surface</Text>
</Left>
<Body>
<Picker
note
inlineLabel={true}
mode={"dropdown"}
style={{width: 175}}
selectedValue={this.state.surface}
onValueChange={this.valueChangeSurface}>
{
this.renderSurface()
}
</Picker>
</Body>
<Right/>
</ListItem>
this.setState
in中返回的错误getSurfaces()
:
提取Feed时出错:[TypeError:未定义不是对象(评估'_this2.setState({表面:responseText.Surfaces.surface})。catch')
我真的不知道我是否采取了好的方法,非常感谢您的帮助
我终于找到了解决方案:
这个问题很简单,大部分来自语法erreur,我将其替换为:
responseText.Surfaces.surface.LIBELLE
这样 :
responseText.Surfaces.surface[i].LIBELLE
查看getSurfaces()的完整代码:
getSurfaces(){ // Input the surfaces values (ID and LIBELLE) who come from WebService in the this.state.surfaces
fetch('url')
.then((response) => response.text())
.then((responseText) => {
parseString(responseText, function (err, result) {
responseText = result;
});
let sf = []; // Create a table who gonna content the value of this.state.surfaces
for(i = 0; i<responseText.Surfaces.surface.length; i++){
sf.push(responseText.Surfaces.surface[i].LIBELLE.toString());
}
this.setState({
surfaces: sf //inject the table sf who contain the table surfaces
});
})
.catch((err) => {
console.log('Error fetching the feed: ', err)
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句