Redux:TypeError:无法读取未定义的属性“ prototype”

时间:2020-01-27 16:09:11

标签: reactjs redux

我正在遵循本教程(https://www.udemy.com/course/mern-stack-front-to-back/)并逐步建立一个现代化的网站。一切顺利,直到“ Profile Reducer&Get Current Profile”。我添加了actions/profile.jsreducers/profile.js等,然后出现以下错误。

我认为我的代码与视频教学的代码相同。有人知道这可能是什么问题吗?

enter image description here

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);

1 个答案:

答案 0 :(得分:1)

从屏幕快照中的堆栈跟踪来看,问题似乎与express软件包有关。

在作为问题一部分提交的代码段中,只有文件actions/profile.js包含与express相关的任何代码。它包含以下import语句:

import { response } from "express";

我看不到您在哪里使用response,所以我认为您不需要它-在删除该行代码后,您承认问题出在注释中。