Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to post, get but not update the items that I create using the form. The items already present in the API database by default can be edited by my form, but not the new ones.

Current local database object on Express.js:
TypeScript
1: {
        id: 1,
        name: 'Jon Doe',
        role: 'Designer',
        age: '1980-08-16',
        image: 'https://randomuser.me/api/portraits/men/1.jpg',
        review: 'Sed nulla nibh, consectetur sit amet lacus ac, euismod  dui quis arcu accumsan mollis.',
        employeesReviews: [
            {
                id: 1,
                name: 'Jack Smith',
                review: 'Sed nulla nibh, consectetur sit amet lacus ac, euismod  dui quis arcu accumsan mollis.'
            }
        ]
},

This is how both default and new items look in the API(http://localhost:7000):
JavaScript
{
    "id": 1,
    "name": "Jon Doe",
    "role": "Designer",
    "age": "1980-08-16",
    "image": "https://randomuser.me/api/portraits/men/1.jpg",
    "review": "Sed nulla nibh, consectetur sit amet lacus ac, euismod  dui quis arcu accumsan mollis.",
    "employeesReviews": [
      {
        "id": 1,
        "name": "Jack Smith",
        "review": "Sed nulla nibh, consectetur sit amet lacus ac, euismod  dui quis arcu accumsan mollis."
      }
    ]
  },
  {
    "id": 5301,
    "name": "New User",
    "role": "Desginer",
    "age": "2021-08-09",
    "image": "https://randomuser.me/api/portraits/men/15.jpg",
    "review": "test review",
    "employeesReviews": [
      
    ]
  }


And this is my code to send the data(I guess the problem is here but I don't now why):
TypeScript
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
    const { name, value } = event.target;
    setUser({ ...user, [name]: value });
  };

  const addUser = () => {

    // User data
    var data = {
      id: Math.floor(Math.random() * 10000 + 1),
      name: user.name,
      role: user.role,
      age: user.age,
      image: user.image,
      review: user.review,
      employeesReviews: []
    };

    // Submit user
    DataService.create(data)
      .then(response => {
        setUser({
          id: response.data.id,
          name: response.data.name,
          role: response.data.role,
          age: response.data.age,
          image: response.data.image,
          review: response.data.review,
          employeesReviews: response.data.employeesReviews
        });
        setSubmitted(true);
        console.log(response.data);
      })
      .catch(e => {
        console.log(e);
      });
  };

  const newUser = () => {
    setUser(initialUserState);
    setSubmitted(false);
  };


When I test the API URL for a item already present(http://localhost:7000/api/menu/items/1) all work fine, but when I try one item that I just created using the form(http://localhost:7000/api/menu/items/5301) it returns: item not found

The odd thing is that when I access all items, the new item is there: http://localhost:7000/api/menu/items/

thanks

What I have tried:

Create a local database/API and update its items using a form. Default database items can be updated, new items can not.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900