Javascript 错误:TypeError:无法读取属性';地图';React Redux中未定义的
我正在使用axios从进行一些简单的获取,并使用redux存储应用程序的状态。从url上传我想得到一个下拉列表中的所有名字。但是给了我两天的错误: TypeError:无法读取未定义的属性“map” 现在,如果您以前看过,我将在下面提供一些代码 这是行动,我认为这是好的Javascript 错误:TypeError:无法读取属性';地图';React Redux中未定义的,javascript,reactjs,redux,react-redux,redux-thunk,Javascript,Reactjs,Redux,React Redux,Redux Thunk,我正在使用axios从进行一些简单的获取,并使用redux存储应用程序的状态。从url上传我想得到一个下拉列表中的所有名字。但是给了我两天的错误: TypeError:无法读取未定义的属性“map” 现在,如果您以前看过,我将在下面提供一些代码 这是行动,我认为这是好的 export const getAuthors = () => { return (dispatch) => { axios.get('https://jsonplaceholder.typicode.com/us
export const getAuthors = () => {
return (dispatch) => {
axios.get('https://jsonplaceholder.typicode.com/users')
.then((response) => {
dispatch({
type: GET_AUTHORS,
payload: response.data
})
})
.catch(e => console.log(e))
}}
这是减速器,我认为这是错误:
export default (state = initialState, action) => {
console.log("ACTION", action.payload);
switch (action.type) {
case GET_AUTHORS:
return {...state, authors: action.payload}
default:
return state;
}
}
以下是显示错误的功能组件:
const AddPostModal = ({authors, authorName}) => {
return(
<select
value={authorName}
onChange={handleChangeAuthor}
{
authors.map((author, index) => {
return (
<option key={index}> {author.name} </option>
)
})
</select>
)
}
export default AddPostModal;
const AddPostModal=({authors,authorName})=>{
返回(
{
返回(
{author.name}
)
})
)
}
导出默认AddPostModal;
下面是类组件:
import { getAuthors} from '../../actions/PostActions';
class Post extends React.Component{
constructor(props) {
super(props);
this.state = {
authorName: ''
}
}
render() {
return(
<AddPostModal
authors={this.props.author}
authorName={this.state.authorName}
/>
)
}
}
const mapStateToProps = (state) => {
return {
authors: state.authorName.authors
};
}
const mapDispatchToProps = (dispatch) => (bindActionCreators({
getAuthors: getAuthors,
}, dispatch))
export default connect(mapStateToProps, mapDispatchToProps)(Post);
const mapStateToProps = (state) => {
return {
authors: state.authorName.authors || []
};
}
从“../../actions/PostActions”导入{getAuthors};
类Post扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
作者名称:“”
}
}
render(){
返回(
)
}
}
常量mapStateToProps=(状态)=>{
返回{
作者:state.authorName.authors
};
}
const mapDispatchToProps=(调度)=>(bindActionCreators({
getAuthors:getAuthors,
}(发送)
导出默认连接(mapStateToProps、mapDispatchToProps)(Post);
如果有人有解决办法,请告诉我,因为你会救我一天。非常感谢您。API异步返回结果,因此您可以首先对
作者进行简单的null
检查。从技术上讲,只能对数组调用.map()
,而null
会抛出您在解决方案中看到的错误
请尝试以下操作:
<select value={authorName}
onChange={handleChangeAuthor}>
{
authors && authors.map((author, index) => {
return <option key={index}> {author.name} </option>
})
</select>
{
authors&&authors.map((作者,索引)=>{
返回{author.name}
})
我希望这会有所帮助!在你的帖子组件中,this.props.author
可以是null
或未定义的,因为this.props.author
不是null
或未定义的,所以我更愿意只在this.props.author
AddPostModal
在Post
组件上:
import { getAuthors} from '../../actions/PostActions';
class Post extends React.Component{
constructor(props) {
super(props);
this.state = {
authorName: ''
}
}
render() {
return(
<AddPostModal
authors={this.props.author}
authorName={this.state.authorName}
/>
)
}
}
const mapStateToProps = (state) => {
return {
authors: state.authorName.authors
};
}
const mapDispatchToProps = (dispatch) => (bindActionCreators({
getAuthors: getAuthors,
}, dispatch))
export default connect(mapStateToProps, mapDispatchToProps)(Post);
const mapStateToProps = (state) => {
return {
authors: state.authorName.authors || []
};
}
首先,确保您的initialState.authors
是[]
。其次,请确保您的状态。mapstateTrops
中的authorName
正确指向您的authorName
还原程序的状态。否则,它可能类似于authors:state.authors
错误已消失,但下拉列表仍然为空。如果我获得consol.log(state),我将映射stateTrops;我将得到以下响应:STATE:{authorName:Array(1),posts:{…}。我知道这是一件小事,但我找不到什么。我假设STATE.authorName
指向来自axios请求的数据。如果是这样,正确的答案是authors:STATE.authorName
,而不是STATE.authorName.authors
。如果您使用console.log(state.authorName)你得到了什么?如果我是console.log(state.authorName)我得到了state:[Array(10)]0:(10)[{…},{…},{…},{…},{…},{…},{…},{…},{…},{…}长度:1 proto:Array(0)好的,数组中有什么?console.log(state.authorName[0])。这是您请求的数据吗?是的,这正是我请求的数组,如果我在返回块中执行author:state.authorName[0],下拉列表仍然为空