Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

reactjs - TypeError: Cannot read property 'payload' of undefined in React js

an getting an error while while submit the form , error are mentioned below..

TypeError: Cannot read property 'payload' of undefined

if you want any more information tell me

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;
   
   }

}

Actions/posts.js

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

//action creators

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);
  
question from:https://stackoverflow.com/questions/65941095/typeerror-cannot-read-property-payload-of-undefined-in-react-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...