HTTP ( PUT request) is not working using axios in React.js

I’m performing a CRUD operation by adding data in db.json server file.
My edit request is not working like I’m using a Class component and having Error ::


import React, { useState, useEffect } from 'react';
import {BrowserRouter as Router, Switch,Route} from 'react-router-dom'
import axios from 'axios';
import Header from './components/header';
import DisplayList from './components/displayList';
import InputForm from './components/InputForm';
import EditContacts from './components/editContacts';
import './App.css';

function App() {
  const [contacts,setContacts] = useState([]);

  const getContacts = async ()=>{
      const res = await axios.get("http://localhost:4000/contacts");

  const addingContacts = async (contact)=>{
    const request = {}
    const response = await"http://localhost:4000/contacts",request)

  const removeContacts = async(id) =>{
   await axios.delete(`http://localhost:4000/contacts/${id}`);
  const newContacts = contacts.filter(contact=> !== id);
//Edit and Update function:
  const updatingContacts = async(contact)=>{
   const response = await 
    const {id,Username,Email} =;
      return !== id ? {} :contact
  return (
        <div className="App">
          <Route path="/" exact>
          <Route path="/add">
          <InputForm addingContacts={addingContacts}/>
          <Route path="/diplay">
          <DisplayList  contacts={contacts} removeContacts={removeContacts}/>

          <Route path="/edit">
          <EditContacts updatingContacts={updatingContacts}/>

export default App;

(editContact.js)Edit Component:

import React, { Component } from 'react';
import{Link} from 'react-router-dom';
import { Form, Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";

class editContacts extends Component {
    constructor(props) {
       const {id,Username,Email} =
        this.state = {
        if(!this.state.Username  && !this.state.Email){
            return alert("Input Fields are Empty");
            alert(`My name is: ${this.state.Username} and my Email address is: ${this.state.Email}`);
    render() {
        return (
            <div className="card" >
                 <div className="card-header text-center">
                 <h2>Add Contacts</h2>
            <Form onSubmit={this.handleUpdate}>
            <Form.Group className="mb-3" controlId="formBasicName">
                    <Form.Control type="text" placeholder="Name" name="Username"value ={this.props.state.Username} onChange={(e)=>{this.setState({})}}/>

                <Form.Group className="mb-3" controlId="formBasicEmail">
                    <Form.Label>Email address</Form.Label>
                    <Form.Control type="email" placeholder="Enter email" name="Email" value ={this.props.state.Email} onChange={(e)=>{this.setState({})}}/>

                <Button variant="btn btn-outline-success" type="submit" style={{marginBottom:"20px", marginRight:"8px"}}>
                <Link to="/">
                <Button variant="btn btn-outline-secondary" type="button" style={{marginBottom:"20px"}}>
                    Main Page

export default editContacts

“Error is occuring in Edit component” Please lemme know if anyone understand this error.

I’m assuming the error occurs when you simply navigate to /edit, is that correct?

Yes !! when I click on edit button then this error occurs:

And here is the code from where list get displayed:

import React from 'react';
import { Link } from 'react-router-dom';

const  displayList = (props) => {
    const  delContact=(id)=>{
        alert("Item is successfully removed");
const returnList =,index)=>{
    return (
                <Link to={{pathname:"/edit", state:{}}}>
                <button className="btn btn-outline-primary" style={{ marginRight: "7px" }}><i className="bi bi-pencil-square"></i></button>
                <button className="btn btn-outline-danger" onClick={()=>delContact(}><i className="bi bi-trash-fill"></i></button>

    return (
       <div className="card" >
        <div className="card-header text-center">
          <h2>Contact List</h2>
        <table className="table table-border table-bordered table-striped text-center">
        <Link to="/">
        <button className="btn btn-outline-secondary">Main Page</button>

export default displayList

   const {id,Username,Email} =

I don’t think props.location is available there like that.

So what supposed I should do ? then ? cause I don’t getting the error

When you browse to /edit/, App.js renders editContacts.js and passes updatingContacts as the only props to the component.
updatingContacts is a function defined in App.js that takes a contact object, updates it on the backend and sets the state to represent the new state of the contacts.
But in your editContacts.js page, you are looking for a location property in props, which you never passed in. So, location is undefined and it has no state property, as the image of the error you provided shows.

Hope that helps

1 Like

So in App.js in this line:
You are passing down the function definition of updatingContacts.
UpdatingContacts is accepting one argument which is “contact”.
So pass contact down into editContact from App.js .

@JasonCorp84 Like how ??

Billy, I don’t mean this to be offensive in anyway, but I think you may lack some react fundamentals. Without them, trying to debug this problem will be in vain.

Have you completed FCC’s frontend development certification with all its projects? If so, have you implemented those projects in react?
I’m asking because, if not, that’s where to start to methodically solve this problem. and everything below.

Also, I would suggest doing the Todo app from to clear up these questions.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.