通过Mobx存储访问时获取未定义

时间:2018-02-02 19:31:36

标签: javascript react-native mobx

我正在尝试访问从服务器获取的json对象的值。 json的内容是这样的:

{
    "id": 1,
    "property_photos": [
        {
            "image_thumbnail": "http://dev.site.com/media/CACHE/images/asdf/24f7d09a44a89e97ae28040bdb0fc059.jpg"
        },
        {
            "image_thumbnail": "http://dev.site.com/media/CACHE/images/sss/656a4131b9201baebb4d0de247c39519.jpg"
        },
    ],
    "address": {
        "url": "http://dev.site.com/places/api/locations/1/",
        "name": "Kenya"
    },
    "title": "3 BHK flat available",
}

我使用mobx作为我的商店。当我从响应数据中访问值时,我可以无错误地访问。但是,在我将响应数据传递到商店并尝试从商店访问该值后,我收到错误

  

TypeError:undefined不是对象

我已经列出了来自respose数据的代码,当我尝试从商店访问数据时也是如此。你能帮帮我吗?

代码:

@inject('PropertyStore') @observer
export default class FeedList extends React.Component {
    componentDidMount() {
        this.fetchProperties()
    }

    fetchProperties = () => {
        const {PropertyStore} = this.props
        axios.get('http://local/api/properties/')
            .then(function (response) {
                console.log('response data0.title', data[0].title)  // Works
                console.log('response data0.address', data[0].address)  // Works
                console.log('response data0.address.name', data[0].address.name)    // Works
                console.log('response data0.property_photos[0]', data[0].property_photos[0])    // Works
            })
    }

    _renderItem = ({item}) => {
        return <FeedCard/>
    }

    render() {
        const {PropertyStore} = this.props
        console.log('PropertyList[0].title', PropertyStore.PropertyList[0].title)   // Works
        console.log('PropertyList[0].address', PropertyStore.PropertyList[0].address)   // Works
        console.log('PropertyList[0].address.name', PropertyStore.PropertyList[0].address.name) // DOES NOT work
        console.log('PropertyList[0].property_photos', PropertyStore.PropertyList[0].property_photos)   // Works
        console.log('PropertyList[0].property_photos', PropertyStore.PropertyList[0].property_photos[0])    // DOES NOT work
        return (
            <View style={styles.container}>
                <FlatList
                    data={PropertyStore.PropertyList}
                    renderItem={this._renderItem}
                />
            </View>
        )
    }
}

0 个答案:

没有答案