How to view the "id" in Json (api)

Asked

Viewed 132 times

-1

I am developing a project in React and I came across a problem, I made a map in a list that works correctly, IE, if I add the objects in the api it appears on the screen, but I would like to create the function "create" with the post method and generate the field "id" in json, how do I do this?

Follow my code below:

import React from 'react';

import api from '../../api/api';

class Experiences extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      experiences: [],
    };
  }

  async componentDidMount() {
    const response = await api.get();
    console.log(response);
    const data = response.data.experiences.map(exp => ({
      ...exp,
    }));

    this.setState({ experiences: data });
  }

  render() {
    const { experiences } = this.state;

    return (
      <>
        {experiences.map(exp => (
          <section key={exp.company} className="content">
            <div className="experience-text">
              <p className="experience-text__first">
                {exp.company}
                <span className="experience__date">{exp.time}</span>
              </p>
              <p className="experience-text_second">{exp.occupation}</p>
              <p className="experience-text_second">{exp.activities}</p>
            </div>
          </section>
        ))}
      </>
    );
  }
}

export default Experiences;

when calling the api in Insomnia, I get the following result

inserir a descrição da imagem aqui

but would like to know how to make a CRUD and get the ID...

thank you very much for your attention

  • So, the correct thing is that your API already returns the ID of this data in the database.

1 answer

0


Thiago, when you send the POST for your endpoint and it save in the database it will automatically generate a unique ID number for your record. To add the ID value of each field you must adjust the GET of your API it must return the ID value along with the other data.

With this you do not need to worry about doing this on your Frontend, as the database itself will already treat the IDS for you!

I hope I helped you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.