как отобразить массив объектов из массива объектов с помощью React - PullRequest
0 голосов
/ 23 сентября 2019

У меня есть этот формат json, когда я console.log(notes):

{
  "document_tone": {
    "tone_categories": [
      {
        "tones": [
          {
            "score": 0.027962,
            "tone_id": "anger",
            "tone_name": "Colère"
          },
          {
            "score": 0.114214,
            "tone_id": "sadness",
            "tone_name": "Tristesse"
          }
        ],
        "category_id": "emotion_tone",
        "category_name": "Ton émotionnel"
      },
      {
        "tones": [
          {
            "score": 0.028517,
            "tone_id": "analytical",
            "tone_name": "Analytique"
          },
          {
            "score": 0,
            "tone_id": "tentative",
            "tone_name": "Hésitant"
          }
        ],
        "category_id": "language_tone",
        "category_name": "Ton de langage"
      },
      {
        "tones": [
          {
            "score": 0.289319,
            "tone_id": "openness_big5",
            "tone_name": "Ouverture"
          },
          {
            "score": 0.410613,
            "tone_id": "conscientiousness_big5",
            "tone_name": "Tempérament consciencieux"
          },
          {
            "score": 0.956493,
            "tone_id": "emotional_range_big5",
            "tone_name": "Portée émotionnelle"
          }
        ],
        "category_id": "social_tone",
        "category_name": "Ton social"
      }
    ]
  },
  "idMedia": 25840
}

это изображение console.log (примечания), я не знаю, почему я получаю пустой массив помимо ожидаемогорезультаты

enter image description here

, но когда я пытаюсь отобразить tone_categories, я получаю эту ошибку:

TypeError: Cannot read property 'map' of undefined

это код IПостроили пока:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

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

    this.state = {
      notes: [],
    };
  }

componentWillMount() {
  fetch('http://localhost:3000/api/users/analyzeMP3?access_token=GVsKNHWnGWmSZmYQhUD03FhTJ5v80BjnP1RUklbR3pbwEnIZyaq9LmZaF2moFbI6', { 
    method: 'post', 
    headers: new Headers({
      'Authorization': 'Bearer', 
      'Content-Type': 'application/x-www-form-urlencoded'
    }), 
  })
    .then(response => response.text())
    .then(JSON.parse)
    .then(notes => this.setState({ notes }));
}

render() {
  const { notes } = this.state;
  console.log('notes',notes)

  return (

    <div className="App">
     {notes !== undefined && notes !== "" &&  notes !== [] ? notes.document_tone.map((tone_categories, idx) => {
    {console.log('notes',notes.document_tone[tone_categories].category_name)}
     }) : null}  

      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

}

export default App;

Ответы [ 4 ]

2 голосов
/ 23 сентября 2019

Ваше начальное состояние notes: [], поэтому массив будет пустым во время первого рендеринга, и если вы попытаетесь получить доступ к элементу из пустого массива, вы получите ошибку.

Лучший подход в этомcase будет иметь состояние загрузки и отложить рендеринг до тех пор, пока вы не получите данные:

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

    this.state = {
      notes: [],
      loading: true // Set the loading to true initially
    };
  }

  componentDidMount() {
    fetch(
      "http://localhost:3000/api/users/analyzeMP3?access_token=GVsKNHWnGWmSZmYQhUD03FhTJ5v80BjnP1RUklbR3pbwEnIZyaq9LmZaF2moFbI6",
      {
        method: "post",
        headers: new Headers({
          Authorization: "Bearer",
          "Content-Type": "application/x-www-form-urlencoded"
        })
      }
    )
      .then(response => response.text())
      .then(JSON.parse)
      .then(notes => this.setState({ notes, loading: false })); // reset the loading when data is ready
  }

  render() {
    const { notes, loading } = this.state;
    console.log("notes", notes);

    return loading ? (
      <p>Loading...</p> // Render the loader if data isn't ready yet
    ) : (
      <div className="App">//...</div>
    );
  }
}
1 голос
/ 23 сентября 2019

Проблема с этим условием notes !== [], которое всегда будет возвращать true.Если вам нужно проверить, является ли массив пустым, вы можете использовать array.length === 0.Также используйте componentDidMount вместо componentWillMount, так как componentWillMount устарел.

Вы можете сделать что-то вроде

return (
    <div className="App">
      {
        notes && notes.length > 0 ? 
        notes.document_tone.map((tone_categories, idx) => {
            return notes.document_tone[tone_categories].category_name;
        }) : null
      }
    </div>
  );
0 голосов
/ 23 сентября 2019

Когда вы запускаете приложение, затем запускается конструктор, вы устанавливаете состояние с помощью notes = [], выполняется render () и печатает его в консоли.

Затем, после willComponentMount (), notesимеет новое значение и запускает новый рендер ().

0 голосов
/ 23 сентября 2019

Это потому, что изначально notes как пустой массив и не имеет ключа document_tone.Таким образом, эта строка будет выдавать ошибку notes.document_tone.map

Добавить условие и проверить, если notes имеют document_tone.

<div className="App">
     {
notes !== undefined && notes !== "" &&  notes !== [] && notes.document_tone.length >0 ?
notes.document_tone.map((tone_categories, idx) => {
    {console.log('notes',notes.document_tone[tone_categories].category_name)}
     }) :
     null}  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...