project_mern_memories
project_mern_memories copied to clipboard
Hey Adrian The fetchpost request not working
So my fetch posts function is not working but if I make a request to the backend in postman it gives the result. the {data}=await fetchPosts() gives undefined This is my actions/posts.js:
`import * as api from "../api/index"
export const getPosts=()=>async(dispatch)=>{ try { const {data}=await api.fetchPosts() dispatch({type:"FETCH_ALL",payload:data}) } catch (error) { console.log(error) } } export const createPosts=(post)=>async(dispatch)=>{ try{ const {data}=await api.createPosts(post) dispatch({type:"CREATE",payload:data}) }catch(error){ console.log(error) } }`
Pls help to fix it
If your code is working with the postman that means ur backend is absolutely fine that means error must be in the fetchPosts() .
please share your fetchpost code
please share your fetchpost code
i am getting a similar issue (i am unable to see posts on home screen but can see after searching by tag or title) idk after vid 4 my code starting breaking
case FETCH_ALL: return { ...state, posts: action.payload.data, currentPage: action.payload.currentPage, numberOfPages: action.payload.numberOfPages, };
export const fetchPost = (id) => API.get(`/posts/${id}`);
export const fetchPosts = (page) => API.get(`/posts?page=${page}`);
export const getPost = (id) => async (dispatch) => {
try {
dispatch({ type: START_LOADING });
const { data } = await api.fetchPost(id);
dispatch({ type: FETCH_POST, payload: { post: data } });
} catch (error) {
console.log(error);
}
};
export const getPosts = (page) => async (dispatch) => {
try {
dispatch({ type: START_LOADING });
const {
data: { data, currentPage, numberOfPages },
} = await api.fetchPosts(page);
dispatch({
type: FETCH_ALL,
payload: { data, currentPage, numberOfPages },
});
dispatch({ type: END_LOADING });
} catch (error) {
console.log(error);
}
};
```