【发布时间】:2021-06-22 15:00:26
【问题描述】:
我正在制作一个博客应用程序。如果我尝试访问单个配置文件,微调器会继续运行,但是当我签入反应开发工具时,数据就在那里。我哪里做错了?其他一切工作正常。配置文件是可见的,但是当我进入个人配置文件时,问题就出现了
Profile.js
import React, { Fragment, useEffect } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import Spinner from "../layout/Spinner";
import { getProfileById } from "../../actions/profile";
import { Link } from "react-router-dom";
const Profile = ({ getProfileById, profile: { profile }, auth, match }) => {
useEffect(() => {
getProfileById(match.params.id);
}, [getProfileById, match.params.id]);
return (
<Fragment>
{profile === null ? (
<Spinner />
) : (
<Fragment>
<Link to='/profiles' className='btn btn-light'>
Back to Profiles
</Link>
{auth.isAuthenticated &&
auth.loading === false &&
auth.user._id === profile.user._id && (
<Link to='/edit-profile' className='btn btn-dark'>
Edit Profile
</Link>
)}
</Fragment>
)}
</Fragment>
);
};
Profile.propTypes = {
getProfileById: PropTypes.func.isRequired,
profile: PropTypes.object.isRequired,
auth: PropTypes.object.isRequired,
};
const mapStateToProps = (state) => ({
profile: state.profile,
auth: state.auth,
});
export default connect(mapStateToProps, { getProfileById })(Profile);
数据的来源,profile.js
import axios from "axios";
import { setAlert } from "./alert";
import {
GET_PROFILE,
PROFILE_ERROR,
UPDATE_PROFILE,
ACCOUNT_DELETED,
CLEAR_PROFILE,
GET_PROFILES,
GET_REPOS,
} from "./types";
export const getCurrentProfile = () => 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,
},
});
}
};
//Get all profiles
export const getProfiles = () => async (dispatch) => {
dispatch({ type: CLEAR_PROFILE });
try {
const res = await axios.get("/api/profile");
dispatch({
type: GET_PROFILES,
payload: res.data,
});
} catch (error) {
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
//Get Profile by id
export const getProfileById = (userId) => async (dispatch) => {
try {
const res = await axios.get(`/api/profile/user/${userId}`);
dispatch({
type: GET_PROFILES,
payload: res.data,
});
} catch (error) {
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
//Get github repos
export const getGithubRepos = (username) => async (dispatch) => {
try {
const res = await axios.get(`/api/profile/github/${username}`);
dispatch({
type: GET_REPOS,
payload: res.data,
});
} catch (error) {
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
//Create or Update profile
export const createProfile =
(formData, history, edit = false) =>
async (dispatch) => {
try {
const config = {
headers: {
"Content-Type": "application/json",
},
};
const res = await axios.post("/api/profile", formData, config);
dispatch({
type: GET_PROFILE,
payload: res.data,
});
dispatch(setAlert(edit ? "Profile Updated" : "Profile Created"));
if (!edit) {
history.push("/dashboard");
}
} catch (error) {
const errors = error.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, "danger")));
}
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
//Add experience
export const addExperience = (formData, history) => async (dispatch) => {
try {
const config = {
headers: {
"Content-Type": "application/json",
},
};
const res = await axios.put("/api/profile/experience", formData, config);
dispatch({
type: UPDATE_PROFILE,
payload: res.data,
});
dispatch(setAlert("Experience Added", "success"));
history.push("/dashboard");
} catch (error) {
const errors = error.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, "danger")));
}
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
//Add Education
export const addEducation = (formData, history) => async (dispatch) => {
try {
const res = await axios.put("/api/profile/education", formData);
dispatch({
type: UPDATE_PROFILE,
payload: res.data,
});
dispatch(setAlert("Education Added", "success"));
history.push("/dashboard");
} catch (error) {
const errors = error.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, "danger")));
}
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
// Delete experience
export const deleteExperience = (id) => async (dispatch) => {
try {
const res = await axios.delete(`/api/profile/experience/${id}`);
dispatch({
type: UPDATE_PROFILE,
payload: res.data,
});
dispatch(setAlert("Experience Deleted", "success"));
} catch (error) {
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
// Delete education
export const deleteEducation = (id) => async (dispatch) => {
try {
const res = await axios.delete(`/api/profile/education/${id}`);
dispatch({
type: UPDATE_PROFILE,
payload: res.data,
});
dispatch(setAlert("Education Deleted", "success"));
} catch (error) {
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
//Delete account & profile
export const deleteAccount = () => async (dispatch) => {
if (
window.confirm("Are you sure? This will be permanently delete the account!")
)
try {
await axios.delete(`/api/profile`);
dispatch({
type: CLEAR_PROFILE,
});
dispatch({
type: ACCOUNT_DELETED,
});
dispatch(setAlert("Account Removed"));
} catch (error) {
dispatch({
type: PROFILE_ERROR,
payload: {
msg: error.response.statusText,
status: error.response.status,
},
});
}
};
【问题讨论】:
标签: javascript reactjs react-redux react-router react-hooks