类型错误:无法读取 React js

时间:2021-01-28 16:18:54

标签: reactjs api redux react-hooks redux-thunk

提交表单时出现错误,错误如下..

<块引用>

TypeError: 无法读取未定义的属性 'payload'

如果你想知道更多信息告诉我

<块引用>

reducer/index.js

export default (posts =[], action) => {
   switch(action.type){
       case 'FETCH_ALL':
            return action.payload;
        case 'CREATE':
            return [...posts. action.payload];
       default:
            return posts;
   
   }

}
<块引用>

操作/posts.js

import * as api from '../api';

//动作创建者

export const getPosts = () => async (dispatch) => {
    try {
        const { data } = await api.fetchPosts();
        dispatch({ type: 'FETCH_ALL', payload: data })
    } catch (error) {
        console.log(error.message)
    }

}

export const createPost = (post) => async (dispatch) =>{
    try{
        const {data} = await api.createPost(post);
        dispatch({type: 'CREATE', payload:data})
    }catch(error){
        console.log(error);
    }
}
<块引用>

API/index.js

import axios from 'axios';

const url = "http://localhost:7000/posts";

export const fetchPosts = () =>  axios.get(url);

export const createPost = (newPost) => axios.post(url,newPost);
  

0 个答案:

没有答案