对象作为React子对象无效(找到:带有键{_id,name}的对象)

时间:2020-05-03 13:36:26

标签: javascript reactjs

错误是在tbody中,并且具有map功能。 代码:

import React, { Component } from "react";
import { getMovies } from "./services/fakeMovieService";

class MovieDatabase extends Component {
  state = {
    movies: getMovies(),
  };

  render() {
    return (
      <table>
        <thead>
          <tr>
            <th>Title</th>
            <th>Genre</th>
            <th>Stock</th>
            <th>Rate</th>
          </tr>
        </thead>
        <tbody>
          {this.state.movies.map((movie) => (
            <tr>
              <td>{movie.title}</td>
              <td>{movie.genre}</td>
              <td>{movie.numberInStock}</td>
              <td>{movie.dailyRentalRate}</td>
            </tr>
          ))}
        </tbody>
      </table>
    );
  }

  listStrecher() {}
}

export default MovieDatabase;

我试图遍历一组对象,当我返回一个元素时它工作正常。但是当我尝试返回多个元素时向我显示了一些错误。 错误: (错误输出错误:对象作为React子对象无效(发现:具有键{_id,name}的对象)。如果要渲染子代的集合,请改用数组。) 检查以下情况:

//Working fine
<tr>
    <td>{movie.title}</td>
</tr>

//Not working(showing Error)
<tr>
    <td>{movie.title}</td>
    <td>{movie.genre}</td>
    <td>{movie.numberInStock}</td>
    <td>{movie.dailyRentalRate}</td>
</tr>

我要迭代的列表


const movies = [
  {
    _id: "5b21ca3eeb7f6fbccd471815",
    title: "Terminator",
    genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
    numberInStock: 6,
    dailyRentalRate: 2.5,
    publishDate: "2018-01-03T19:04:28.809Z"
  },
  {
    _id: "5b21ca3eeb7f6fbccd471816",
    title: "Die Hard",
    genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
    numberInStock: 5,
    dailyRentalRate: 2.5
  },
  {
    _id: "5b21ca3eeb7f6fbccd471817",
    title: "Get Out",
    genre: { _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" },
    numberInStock: 8,
    dailyRentalRate: 3.5
  },
  {
    _id: "5b21ca3eeb7f6fbccd471819",
    title: "Trip to Italy",
    genre: { _id: "5b21ca3eeb7f6fbccd471814", name: "Comedy" },
    numberInStock: 7,
    dailyRentalRate: 3.5
  },
  {
    _id: "5b21ca3eeb7f6fbccd47181a",
    title: "Airplane",
    genre: { _id: "5b21ca3eeb7f6fbccd471814", name: "Comedy" },
    numberInStock: 7,
    dailyRentalRate: 3.5
  },
  {
    _id: "5b21ca3eeb7f6fbccd47181b",
    title: "Wedding Crashers",
    genre: { _id: "5b21ca3eeb7f6fbccd471814", name: "Comedy" },
    numberInStock: 7,
    dailyRentalRate: 3.5
  },
  {
    _id: "5b21ca3eeb7f6fbccd47181e",
    title: "Gone Girl",
    genre: { _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" },
    numberInStock: 7,
    dailyRentalRate: 4.5
  },
  {
    _id: "5b21ca3eeb7f6fbccd47181f",
    title: "The Sixth Sense",
    genre: { _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" },
    numberInStock: 4,
    dailyRentalRate: 3.5
  },
  {
    _id: "5b21ca3eeb7f6fbccd471821",
    title: "The Avengers",
    genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
    numberInStock: 7,
    dailyRentalRate: 3.5
  }
];

export function getMovies() {
  return movies;
}

2 个答案:

答案 0 :(得分:2)

movie.genre返回一个对象。如错误所示,您无法在React中做到这一点。只需从genre对象中选择所需的信息,然后仅渲染类似movie.genre.name的信息即可。

答案 1 :(得分:1)

尝试获取genre.name

import React, { Component } from "react";
import { getMovies } from "./services/fakeMovieService";

class MovieDatabase extends Component {
  state = {
    movies: getMovies(),
  };

  render() {
    return (
      <table>
        <thead>
          <tr>
            <th>Title</th>
            <th>Genre</th>
            <th>Stock</th>
            <th>Rate</th>
          </tr>
        </thead>
        <tbody>
          {this.state.movies.map((movie) => (
            <tr>
              <td>{movie.title}</td>
              {/*Get the name of the genre here*/}
              <td>{movie.genre.name}</td>
              <td>{movie.numberInStock}</td>
              <td>{movie.dailyRentalRate}</td>
            </tr>
          ))}
        </tbody>
      </table>
    );
  }

  listStrecher() {}
}

export default MovieDatabase;