首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未定义的TypeError:无法读取未定义的属性(读取‘params’)(使用类组件)

未定义的TypeError:无法读取未定义的属性(读取‘params’)(使用类组件)
EN

Stack Overflow用户
提问于 2022-02-28 14:01:06
回答 2查看 270关注 0票数 0

我通过构建一个运动跟踪器来练习我的MERN技能,然后我遇到了这样的错误:"Uncaught :无法读取未定义的属性(阅读'params')"。最近,我找到了一个指导我使用"useParams“钩子的解决方案。但这对我来说没有用,因为我使用的是类组件。下面是造成问题的代码块:

代码语言:javascript
运行
复制
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css"
import axios from "axios";
import { Params } from "react-router-dom";

export default class EditExercises extends Component {
    constructor(props) {
        super(props);

        this.onChangeUsername = this.onChangeUsername.bind(this);
        this.onChangeDescription = this.onChangeDescription.bind(this);
        this.onChangeDuration = this.onChangeDuration.bind(this);
        this.onChangeDate = this.onChangeDate.bind(this);
        this.onSubmit = this.onSubmit.bind(this);

        this.state = {
            username: "",
            description: "",
            duration: 0,
            date: new Date(),
            users: []
        }
    }

    componentDidMount() {
        axios.get("http://localhost:5000/exercises/" + this.props.match.Params.id)
            .then(response => {
                this.setState({
                    username: response.data.username,
                    description: response.data.description,
                    duration: response.data.duration,
                    date: new Date(response.data.date)
                })
            })
            .catch(error => console.log(error));
EN

回答 2

Stack Overflow用户

发布于 2022-02-28 14:05:19

您的路线有问题,请您共享该文件。它有路线:id在里面吗?像这样

如果没有,请添加它,因为路由器在那里找不到它。如果在那里,请将Params的名称改为params (小写的)。

票数 0
EN

Stack Overflow用户

发布于 2022-02-28 14:05:30

它必须是对线而不是傍线。

代码语言:javascript
运行
复制
axios.get("http://localhost:5000/exercises/" + this.props.match.params.id)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71296161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档