【问题标题】:Redux: TypeError: Cannot read property 'prototype' of undefinedRedux:TypeError:无法读取未定义的属性“原型”
【发布时间】:2020-05-13 01:08:07
【问题描述】:

我正在按照本教程 (https://www.udemy.com/course/mern-stack-front-to-back/) 逐步建立一个 mern 网站。一切都很顺利,直到“Profile Reducer & Get Current Profile”。我添加了actions/profile.jsreducers/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 包中引发错误。你试过一步步调试吗?
  • 您能添加一个答案以便我接受吗?

标签: reactjs redux


【解决方案1】:

从屏幕截图中的堆栈跟踪来看,问题似乎与 express 包有关。

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

import { response } from "express";

我看不到你在哪里使用 response,所以我认为你不需要它 - 这是你承认问题来自 cmets 后删除那行代码的地方。

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2021-08-01
    • 2021-10-30
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    相关资源
    最近更新 更多