0

I receive this error when trying to edit an 'exercise' The error points to line 28 in exercises.component.js shown below:

axios.get('http://localhost:5000/exercises/' + this.props.match.params.id)

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

    this.onChangeUsername = this.onChangeUsername.bind(this);
    this.onChangeWorkout = this.onChangeWorkout.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: '',
      workout: '',
      description: '',
      duration: 0,
      date: new Date(),
      users: []
    }
  }

  componentDidMount() {
    axios.get('http://localhost:5000/exercises/' + this.params.id)
      .then(response => {
        this.setState({
          username: response.data.username,
          workout: response.data.workout,
          description: response.data.description,
          duration: response.data.duration,
          date: new Date(response.data.date)
        })
      })
      .catch(function(error) {
        console.log(error);
      })

    axios.get('http://localhost:5000/users/')
      .then(response => {
        if (response.data.length > 0) {
          this.setState({
            users: response.data.map(user => user.username),
          })
        }
      })
      .catch((error) => {
        console.log(error);
      })

  }

I acknowledge from another similar question with response that params is outdated from the tutorial I had been working from and useParams() must be used instead in the version 5 and abover in React. However I cannot seem to figure out how to include useParams() to be able to pass in the id and edit the document.

Any help would be greatly appreciated.

My App.js :

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import "bootstrap/dist/css/bootstrap.min.css"


import Navbar from "./components/navbar.component";
import ExercisesList from "./components/exercises-list.component";
import CreateExercise from "./components/create-exercise.component";
import CreateUser from "./components/create-user.component";
import EditExercises from "./components/edit-exercises.component";
import CreateMealPlan from "./components/create-meal-plan.component";
import EditMealPlan from "./components/edit-meal-plan.component";
import MealPlanList from "./components/meal-plan-list.component"

function App() {
  return (
    <Router>
      <div className="container">
        <Navbar />
        <br/>
        <Routes>
          <Route path="/" exact element={<ExercisesList/>} />
          <Route path="/meal-plan" exact element={<MealPlanList/>} />
          <Route path="/edit/:id" element={<EditExercises/>} />
          <Route path="/editMeal/:id" element={<EditMealPlan/>} />
          <Route path="/create" element={<CreateExercise/>} />
          <Route path="/createMeal" element={<CreateMealPlan/>} />
          <Route path="/user" element={<CreateUser/>} />
        </Routes>
      </div>
    </Router>
  );
}

export default App;

First question on StackOverflow appreciate any help and hope I can respond to any comments correctly :)

andrewnev
  • 1
  • 4

0 Answers0