pydeveloper
pydeveloper

Reputation: 11

Can't retreive my movies data after importing array function in React JS

I can't pass my data from the fakeGenreService.js via array.

Please check the screenshot for the data rendered.

You will see that all things are being rendered, just not (the movie Title, Genre, Stock and Rate) which are available in the fakeGenreService.js

Please do let me know where I am going wrong??????

PLEASE DO LET ME KNOW WHY MY DATA IS NOT BEING RENDERED AND WHAT I NEED TO MAKE THE CHANGES IN THE CODE I WILL REALLY APPRECIATE YOUR HELP!!!!!!

I am uploading my three files below

  1. App.js
  2. fakeGenreService.js
  3. movies.js

Please check if I am passing the array correctly in the state block?????``

Here is App.js http://prnt.sc/olccj9

Here is fakegenreService.js http://prnt.sc/olcdr5

Here is movies.js http://prnt.sc/olce2x

Here is the final result for the developmentserver http://prnt.sc/olcejx

Tried various troubsleshooting steps for the array function

This part deals with App.js

import React, { Component } from "react";
import React, { Component } from "react";
import Movies from "./components/movies";
import "./App.css";

class App extends Component {
  render() {
   return (
      <main className="container">
        <Movies />
      </main>
    );
  }
}

export default App;

This part is for movies.js

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

class Movies extends Component {
  constructor(props) {
    super(props); 
    this.state = {
                movies: [getMovies()]

     };
   }

  handleDelete = movie => {
    console.log(movie);
  };

  render() {
        return (
      <table className="table">
        <thead>
          <tr>
            <th>Title</th>
            <th>Genre</th>
            <th>Stock</th>
            <th>Rate</th>
            <th />
          </tr>
        </thead>
        <tbody>
          {this.state.movies.map(movie => (
            <tr key={movie._id}>
              <td>{movie.title}</td>
              <td>{movie.genre}</td>
              <td>{movie.numberInStock}</td>
              <td>{movie.dailyRentalRate}</td>
              <td>
                <button
                  onCick={() => this.handleDelete(movie)}
                  className="btn btn-danger btn-sm"
                >
                 Delete
                </button>
             </td>
            </tr>
          ))}
        </tbody>
      </table>
    );
  }
}
export default Movies;

Here is fakeMovieService.js

import * as genresAPI from "./fakeGenreService";

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;
}

export function getMovie(id) {
  return movies.find(m => m._id === id);
}

export function saveMovie(movie) {
  let movieInDb = movies.find(m => m._id === movie._id) || {};
  movieInDb.name = movie.name;
  movieInDb.genre = genresAPI.genres.find(g => g._id === movie.genreId);
  movieInDb.numberInStock = movie.numberInStock;
  movieInDb.dailyRentalRate = movie.dailyRentalRate;

  if (!movieInDb._id) {
    movieInDb._id = Date.now();
    movies.push(movieInDb);
  }

  return movieInDb;
}

export function deleteMovie(id) {
  let movieInDb = movies.find(m => m._id === id);
  movies.splice(movies.indexOf(movieInDb), 1);
  return movieInDb;
}


The result of the data being rendered is shown here: http://prnt.sc/olcejx

Please let me know how could the movies defined in getMovies() function coud be rendered in the table.

Upvotes: 1

Views: 181

Answers (3)

Shashank
Shashank

Reputation: 324

getMovies() already returning array. You are calling that function inside an array. so movies have an array of array. like this movies: [[datas]]. In movies.js file do this changes in the constructor. It should work.

this.state = {
  movies: getMovies();
}

Upvotes: 0

Domino987
Domino987

Reputation: 8774

You wrap the movies array into a second array. That does not work. You should write it like this :

this.state = {
   movies: getMovies()
 };

Upvotes: 0

SiddAjmera
SiddAjmera

Reputation: 39432

The issue seems to be here. getMovies would already return an array. You're wrapping it inside another one. Here, in yout Movies Component class, change it to just the function call:

constructor(props) {
  super(props);
  this.state = {
    movies: getMovies() // [getMovies()]
  };
}

Upvotes: 1

Related Questions