I am working through a full stack project. So far, I have created a login and register which both work and they return a jwt web token. I am trying to make a GET request to (/api/profile/me) to get the logged in user profile but I get a 400 error. It works in Postman but fails in react.
actions - profile.js
import axios from 'axios';
import { GET_PROFILE, PROFILE_ERROR } from './types';
// Get current users profile
export const getCurrentProfile = () => {
return async (dispatch) => {
try {
const res = await axios.get('/api/profile/me');
dispatch({
type: GET_PROFILE,
payload: res.data,
});
} catch (error) {
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
};
reducers - profile.js
import { PROFILE_ERROR, GET_PROFILE } from '../actions/types';
const initialState = {
profile: null,
profiles: [],
repos: [],
loading: true,
error: {},
};
export default function (state = initialState, action) {
const { type, payload } = action;
switch (type) {
case GET_PROFILE:
return {
...state,
profile: payload,
loading: false,
};
case PROFILE_ERROR:
return {
...state,
error: payload,
loading: false,
};
default:
return state;
}
}
Dashboard where I want to load the action
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { useSelector, useDispatch } from 'react-redux';
import { getCurrentProfile } from '../../actions/profile';
const Dashboard = () => {
const auth = useSelector((state) => state.auth);
const profile = useSelector((state) => state.profile);
const dispatch = useDispatch();
useEffect(() => {
dispatch(getCurrentProfile());
}, []);
return (
<div className="container">
<p className="dashboard-heading">Dashboard</p>
</div>
);
};
export default Dashboard;
I found out the issue by console logging the error response data. It returned the message
"There is no profile for this user"
meaning that I used a different user than the one I used in Postman.