TypeError:无法读取未定义反应的属性“ url”

时间:2019-11-06 20:29:25

标签: reactjs

有人可以帮忙吗?我得到了AddPost.js文件

</div>
                            {this.props.admin.post.PostImage ?
                                <img src={API.makeFileURL(this.props.admin.post.PostImage[0].url, this.props.auth.token)} className="post-image" /> 
                            : null}
                            <div>
                                <Button
                                    variant="contained" 
                                    color="primary"
                                    onClick={e => {
                                        $('.MyFile').trigger('click'); 
                                    }}
                                ><ImageIcon />Upload Post Image</Button>
                                <input type="file" style={{display: 'none'}} className="MyFile" onChange={this.uploadImage}/>
                            </div>

API文件包含此功能:

makeFileURL: (url, token) => {
        return host + url + "?access_token" + token;
    }

3 个答案:

答案 0 :(得分:1)

import {Link,Route} from 'react-router-dom';
export const Category = ({match}) => {
    return (
        <div>
            <ul>
                <li><Link to={`${match.url}/mobile`} >Mobile</Link></li>
                <li><Link to={`${match.url}/laptop`} >Laptop</Link></li>
                <li><Link to={`${match.url}/shoes`} >Shoes</Link></li>
            </ul>
           
        </div>
    )
}

在这种情况下,任何人都会收到错误“TypeError: Cannot read property 'url' of undefined” 所以需要改变

<Route path='/category' component={Category} />

答案 1 :(得分:0)

java.lang.IllegalStateException: View androidx.core.widget.NestedScrollView{1dd5506 VFED..... ......I. 0,0-0,0} does not have a NavController set this.props.admin.post的评价是什么?我将从this.props.admin.post.PostImage开始,并确保它返回具有console.log属性的对象数组

答案 2 :(得分:0)

试试这个

this.props.admin.post.PostImage&&
this.props.admin.post.PostImage[0]&&
this.props.admin.post.PostImage[0].url