Estoy trabajando en un proyecto de pila completa. Hasta ahora, he creado un inicio de sesión y un registro que funcionan y devuelven un token web jwt. Estoy tratando de hacer una solicitud GET a (/api/profile/me) para obtener el perfil de usuario registrado, pero aparece un error 400. Funciona en Postman pero falla al reaccionar.
acciones - perfil.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, }, }); } }; };reductores - perfil.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 donde quiero cargar la acción
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;Descubrí el problema al registrar la consola los datos de respuesta del error. Devolvió el mensaje
"No hay perfil para este usuario"
lo que significa que usé un usuario diferente al que usé en Postman.