简体   繁体   English

React Native无法呈现获取的数据。 抛出未定义不是对象

[英]React Native can not render fetched data. Throws Undefined is not an Object

I'm currently working on react native project. 我目前正在研究本机项目。

I'm getting data with axios in componentDidMount function, but data is SOAP way. 我在componentDidMount函数中使用axios获取数据,但是数据是SOAP方式。 Therefore I'm changing the data xml to json with xml-js package. 因此,我要使用xml-js包将数据xml更改为json。

Here is my state; 这是我的状态;

state = {
    contacts: [],
    isLoading: true
} 

Here is my componentDidMount() function; 这是我的componentDidMount()函数;

componentDidMount() {
    xmls = 'my envelope to server.';
    Axios.post('my url goes here.', xmls, {
        headers: {
            'Content-Type': 'text/xml; charset=UTF-8',
            'Authorization': 'my key',
            'SOAPAction': "my action"
        }
    }).then(response => {
        return response.data;
    }).then((res) => {
        const options = {
            compact: true,
            ignoreDeclaration: true,
            spaces: 4,
            ignoreAttributes: true,
            textFn: this.removeJsonTextAttribute,
            elementNameFn: function (val) { return val.replace('SOAP-ENV:', '').replace('ns:', ''); } //removes 'SOAP-ENV: and ns: tags from SOAP data.
        }
        // converting string xml to string json.
        let xmlToJSON = convert.xml2json(res, options);
        // converting string to json with JSON.parse()
        return contactsObj = JSON.parse(xmlToJSON);
    }).then((fin) =>{
        this.setState({contacts: fin, isLoading: false});
    }).catch(err => {
        console.warn('Error', err);
    });
}

Here is my render function; 这是我的渲染功能;

render() {
    let arr = [];
    arr.push(this.state.contacts);

    let {isLoading} = this.state;


    let res = arr.map((item, i)=> {
        return <AnnounceList headLine = {item[0].Envelope.Body.a.b.c[i]} />
     });

    return (
        <View style = {{flex: 1}}>
            <TopBar name={"Iletisim"} bColor={"#29303c"} miniLogo={require('../../../assets/pictures/Ikon07-tip2-0.png')} />
            <Spinner loading = {isLoading} color = '#29303c'>
                <ScrollView>
                {res}                      
                </ScrollView>
            </Spinner>
        </View>
    );
}

For rendering my data I'm creating an array instance and push my state contacts data to there. 为了渲染数据,我创建了一个数组实例并将状态联系人数据推送到该实例。 But when I'm trying to select data from array it is throwing me undefined is not an object error. 但是,当我尝试从数组中选择数据时,抛出未定义的错误不是对象错误。 Data is valid until Envelope but after Envelope.Body it throws an error. 数据在信封之前有效,但是在信封之后,它会引发错误。 I didn't understand why it is throwing? 我不明白为什么要扔? Any idea. 任何想法。 Thanks for all contribution. 感谢您的所有贡献。

Added Information!! 补充信息!

In my state, contacts is an array. 在我的状态下,联系人是一个数组。 but in my render function when I try to this.state.contacts.map() it throws me undefined is not function so I checked out what is type of it with console.log(typeof this.state.contacts) it returns object. 但是在我的渲染函数中,当我尝试使用this.state.contacts.map()它使我无法定义是不起作用的,因此我使用console.log(typeof this.state.contacts)检查了它的类型,它返回了对象。 Why did it become an object? 为什么它成为对象? Mustn't it stay as an array? 难道它不能作为数组保留吗?

I think the problem is here 我认为问题出在这里

 let res = arr.map((item, i)=> {
    return <AnnounceList headLine = {item[0].Envelope.Body.a.b.c[i]} />
 });

You are looping through all item you have and you are taking item[0]? 您正在遍历所有物品,正在拿物品[0]? why? 为什么? Then you are taking i which is index of element. 然后,您将i作为元素的索引。 Why not to use item? 为什么不使用物品? Adding something like: 添加类似:

 let res = arr.map((item, i)=> {
    return <div>JSON.stringify(item)</div>
 });

will help you with debugging. 将帮助您进行调试。 You can also make your json more beautifull with: 您还可以使用以下方法使json更美观:

return <div><pre>{JSON.stringify(item, null, 2) }</pre></div>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM