1

So I have a database set up and I am trying to pull the info from it and store it into a state array so I can use it on the website.

This is an example of what I get when I request form the database, shortened for readability

{
    "count": 10,
    "next": null,
    "previous": null,
    "results": [
        {
            "var1": "Test",
            "var2": "",
            "NeededInfo": "name1",
        },
        {
            "var1": "Test",
            "var2": "",
            "NeededInfo": "name2",
        }
    ]
}

So what I need is to take the NeededInfo of each object and store it into an array so I can use it on render(). This is what I'm trying, but when I do printToConsole() (not shown but just a console.log(this.state)) to see what the state array looks like, the "other" array is undefined

const title_URL = "http://123.456.789/namelist";

class Project extends Component {
  constructor(props) {
    super(props);

    this.state = {
      random1: "",
      random2: "",
      other: [],
      items: []
    }
  }

  componentDidMount() {
    fetch(title_URL)
      .then(response => {
        return response.json();
      })
      .then(data => {
        this.setState({
          other: data.results.NeededInfo
        })
      });
  }

I need the "other" array to look like

other: ["name1", "name2"]

because in my render() I have to list the names that the site pulls from the database so the user can see the names of the listed items and do some other stuff with it, and then I can save that into the "items" array, in a new order, but that part works with if I have a preset "other" array.

1
  • You can mark and accept the answer if its works fine, so others can see it, Thanks Commented Jun 27, 2019 at 9:13

3 Answers 3

2

You need to get NeededInfo from your result and push it into a temporary array (store NeededInfo values only), then just set it into state. For example:

componentDidMount() {
    fetch(title_URL)
        .then(response => {
            return response.json();
        })
        .then(data => {
            // Here you need to use an temporary array to store NeededInfo only 
            let tmpArray = []
            for (var i = 0; i < data.results.length; i++) {
                tmpArray.push(data.results[i].NeededInfo)
            }

            this.setState({
                other: tmpArray
            })
        });
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use map and extract the wanted property:

other: data.results.map(({ NeededInfo }) => NeededInfo)

Comments

1

Results is an array of documents.

You cannot access the property of documents inside that array without telling JavaScript that where is that documents inside the array ( index of needed document ).

Create for loop , use I start from 0 , to length of results , and you will be able to use that documents property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.