【发布时间】:2020-05-13 01:08:07
【问题描述】:
我正在按照本教程 (https://www.udemy.com/course/mern-stack-front-to-back/) 逐步建立一个 mern 网站。一切都很顺利,直到“Profile Reducer & Get Current Profile”。我添加了actions/profile.js和reducers/profile.js等,我得到了以下错误。
我认为我的代码与视频所教的相同。有谁知道可能是什么问题?
reducers/profile.js:
import {
GET_PROFILE,
PROFILE_ERROR
} 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
}
}
actions/profile.js
import axios from "axios";
import { setAlert } from "./alert";
import { GET_PROFILE, PROFILE_ERROR } from "./types";
import { response } from "express";
// Get current users profile
export const getCurrentProfile = () => async dispatch => {
try {
const res = await axios.get("/api/profile/me");
dispatch({
type: GET_PROFILE,
payload: res.data
});
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
}
};
Dashboard.js:
import React, { useEffect } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import { getCurrentProfile } from "../../actions/profile";
const Dashboard = ({ getCurrentProfile, auth, profile }) => {
useEffect(() => {
getCurrentProfile();
}, []);
return <div>Dashboard</div>;
};
Dashboard.propTypes = {
getCurrentProfile: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired,
profile: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
auth: state.auth,
profile: state.profile
});
export default connect(mapStateToProps, { getCurrentProfile })(Dashboard);
【问题讨论】:
-
我们不知道本课程使用什么代码。请在此处粘贴相关内容,以便我们帮助调试问题
-
嗯,这个项目很大,有很多文件。
-
好的,但我希望你能理解我们无法通过猜测代码的样子来帮助解决问题
-
你需要
import { response } from "express";吗?您似乎没有在actions/profile.js中使用它。堆栈跟踪似乎从express包中引发错误。你试过一步步调试吗? -
您能添加一个答案以便我接受吗?