简体   繁体   English

我的代码中有错误反应 js 项目

[英]i have an error in my code react js project

The error is, TypeError: Cannot read properties of undefined (reading 'results') please help me i want to finish the project.错误是,类型错误:无法读取未定义的属性(读取“结果”)请帮助我完成项目。 I have used API from movie database inorder to fetch data of the movies.我使用了电影数据库中的 API 来获取电影的数据。 but in so doing i get the errors, you cannot see them in the code editor, i use visual studio code but when i save, it doesn't show the results and it has challenged me.但是这样做时我得到了错误,您无法在代码编辑器中看到它们,我使用 Visual Studio 代码,但是当我保存时,它没有显示结果,这对我提出了挑战。 I am going to quit the project because i can no longer go further more.我要退出这个项目,因为我不能再进一步了。 I need some help.我需要帮助。 Yes i can understand that i am new in this react but i have practiced it for now 3 months.是的,我可以理解我是这个反应的新手,但我已经练习了 3 个月。 I have not posted other codes but if in need i will have to post the whole project.我没有发布其他代码,但如果需要,我将不得不发布整个项目。 Some help please and i will appriciate.请一些帮助,我会appriciate。

Home.js:27 Uncaught TypeError: Cannot read properties of undefined (reading 'results')
at Home (Home.js:27)
at renderWithHooks (react-dom.development.js:14985)
at mountIndeterminateComponent (react-dom.development.js:17811)
at beginWork (react-dom.development.js:19049)

home.js主页.js

import React, { useState, useEffect } from "react"; //rafce
import reactDom from "react-dom";

//Config
import { POSTER_SIZE, BACKDROP_SIZE, IMAGE_BASE_URL } from "../config";
//components
import HeroImage from "./HeroImage";
//hook
import { useHomeFetch } from "../hooks/useHomeFetch";
//image
import NoImage from "../images/no_image.jpg";
  
import Grid from "./Grid";

import Thumb from "./Thumb";

import Spinner from "./Spinner";

import SearchBar from "./SearchBar";

const Home = () => {
  const { state, loading, error, setSearchTerm } = useHomeFetch();

  console.log(state);

  return (
    <>
      {state.results ? (
        <HeroImage
          image={`${IMAGE_BASE_URL}${BACKDROP_SIZE}${state.results[0].backdrop_path}`}
          title={state.results[0].original_title}
          text={state.results[0].overview}
        />
      ) : null}
      <SearchBar setSearchTerm={setSearchTerm} />
      <Grid header="Popular Movies">
        {state.results.map((movie) => (
          <Thumb
            key={movie.id}
            clickable
            image={
              movie.poster_path
                ? IMAGE_BASE_URL + POSTER_SIZE + movie.poster_path
                : NoImage
            }
            movieId={movie.id}
          />
        ))}
      </Grid>
      <Spinner />
    </>
  );
}; 

export default Home;

useHomeFetch.js使用HomeFetch.js

import { useState, useEffect, useRef } from "react";
import reactDom from "react-dom";
//API
import API from "../API";
//initial state
const initialState = {
  page: 0,
  results: [],
  total_pages: 0,
  total_results: 0,
};
export const useHomeFetch = () => {
  const [searchTerm, setSearchTerm] = useState("");
  const [state, setState] = useState();
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(false);

  const fetchMovies = async (page, searchTerm = "") => {
    try {
      setError(false);
      setLoading(true);

      const movies = await API.fetchMovies(searchTerm, page);

      setState((prev) => ({
        ...movies,
        results:
          page > 1 && prev ? [...prev.results, ...movies.results] : [...movies.results],
      }));
    } catch (error) {
      setError(true);
    }
    setLoading(false);
  };

  //initial ans search
  useEffect(() => {
    // setState(initialState);
    fetchMovies(1);
  }, []);

  return { state, loading, error, setSearchTerm };
}; 

The first time when you setState the prev is undefined, but you don't have check for that第一次当你 setState prev 是未定义的,但你没有检查

Try something like that:尝试这样的事情:

results: page > 1 && prev ? [...prev.results, ...movies.results] : [...movies.results]

you are missing the 'movies' after prev.你错过了上一个之后的“电影”。 it should be:它应该是:

page > 1 && prev ? [...prev.results, ...movies.results] : [...movies.results],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在我的 JS 代码中编写了 fadeOut 命令,在我的待办事项列表项目中,但是淡出不能正常工作并在谷歌控制台中显示错误? - I have written fadeOut command in my JS code, in my to do list project but the fadeout is not working properly and showing error in google console? 我的 Vue.js 项目中有一个 ESlint 错误,为什么我不能修复它 - I have an ESlint error in my Vue.js project, why can't I fix it 我学校javascript项目出错 - I have an error in my school javascript project 为什么我在系统中第一次运行 react js 项目时收到此错误消息? - Why I am getting this error message when I run react js project first time in my system? 为什么我在 node.js 代码中使用 sql 时出现语法错误 - why i have syntax error when i use sql in my node.js code 我的 jsconfig.json 在反应 js 中出现错误,在 position 的 JSON 中说 throw err Unexpected token } - I have an error with my jsconfig.json in react js says throw err Unexpected token } in JSON at position 我尝试使用3反应导航版本运行我的drawerNavigation代码,但仍然说错误 - I have tried running my drawerNavigation code with 3 react navigation version but still says error 我的JavaScript代码正常运行,但我有错误。 - My JavaScript code is working , but I have error . 我在我的 three.js 文件中添加了 GUI 代码,但它不起作用,也没有引发任何错误 - I have added the GUI code in my three js file but it's not working not raising any error either 我的项目中是否可以有多个gruntjs文件用于代码组织? - Can I have multiple gruntjs files in my project for code organization?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM