简体   繁体   English

如何解决反应项目中显示的此错误 - 未捕获(承诺)TypeError:无法读取未定义的属性(读取'id')

[英]How to solve this error showing in react project - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'id')

import React ,{useEffect, useState}from 'react'

import Layout from '../components/Layout'

import axios from 'axios';

import TinderCard from 'react-tinder-card'

import MovieCard from '../components/MovieCard';

import { createClient } from '@supabase/supabase-js'

import { useAuth } from '../auth';


function Home() {

  const auth = useAuth();
  const supabaseUrl = process.env.REACT_APP_SUPABASE_URL
  const supabaseKey = process.env.REACT_APP_SUPABASE_KEY
  const supabase = createClient(supabaseUrl, supabaseKey)
  const [movies, setMovies] = useState([])
  const [message, setMessage] = useState("")
  
 const fecthMovies = async () => {
  const {data} = await axios.get("https://api.themoviedb.org/3/discover/movie",{
    params:{
      page:Math.random() * 501,
      api_key:"57e19e5c44a33653ce6bfc54743c9e2e"
    }
  })
 console.log(data);
const movie = await data;
setMovies(movie.results);
}
const addTowatchlist = async (movie) => {

const {data, error} = await supabase.from("watchlists").insert({movie_id : movie.id, user_id: auth.user.id})
if(error){
  console.log(error)
}
if(data){
  setMessage("Movie has been added to your watchlist")
}
}

 useEffect(() => {
  if(auth.user){
    fecthMovies()
  }
  
 
 },[auth])





return (
  <Layout>
    {message&&message}
    <h1>Welcome</h1>
    {!auth.user && <h2>Please sign up</h2>}

{movies.map(movie => {
 return <>
 <div className="movie-wrapper">
 <TinderCard
 onSwipe={direction => direction === "right"? addTowatchlist():null}
 key={movie.id}>
    <MovieCard movie={movie}/>
  
  </TinderCard>
 </div>
 
 </>
})}

  </Layout>
 )
 

}

export default Home

这是错误

The above given is my Home.js.上面给出的是我的 Home.js。 I don't know why it is showing undefined.我不知道为什么它显示未定义。 I tried to change the 'movies' in 'movies.map' to 'Movies' and so but it didn't do that much.我试图将 'movies.map' 中的 'movies' 更改为 'Movies' 等等,但它并没有做那么多。 This error is happening when iam trying to add a item to the watchlist table i created in supabase database.当我尝试将项目添加到我在 supabase 数据库中创建的监视列表表时,会发生此错误。 And i need to add the movie 'id' iam getting from the item to watchlist table.我需要将从项目中获取的电影“id”添加到监视列表表中。 when i tried this当我尝试这个

const {data, error} = await supabase.from("watchlists").insert({movie_id : movie?.id, user_id: auth.user.id})

like adding?喜欢添加? in 'movie.id' it works but i don't get the id because of the?在“movie.id”中它有效但我没有得到 id 因为? . . So how can i add the movie id to the list by fixing this error.那么如何通过修复此错误将电影 ID 添加到列表中。 I really appreciate the help.非常感谢您的帮助。 thanks in advance.提前致谢。

The?.这?。 operator is like the.算子就好了。 chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined.链接运算符,除了如果引用为空(null 或 undefined)时不会导致错误,表达式会短路并返回 undefined 值。 When used with function calls, it returns undefined if the given function does not exist.当与 function 调用一起使用时,如果给定的 function 不存在,它将返回未定义。

so in the below code user_id: auth?.user?.id it will not be accessible until it is nullish and if it's null it will return undefined instead of crashing an application.所以在下面的代码中 user_id: auth?.user?.id 直到它为 nullish 时才可以访问它,如果它是 null 它将返回 undefined 而不是使应用程序崩溃。

const {data, error} = await supabase.from("watchlists").insert({movie_id : movie?.id, user_id: auth?.user?.id})

暂无
暂无

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

相关问题 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'id') - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'id') 错误:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“数据”) - ERROR: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data') 如何处理 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading - how to handle Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 如何解决未捕获的类型错误:无法读取未定义的属性(读取“切换”) - How to solve Uncaught TypeError: Cannot read properties of undefined (reading 'toggle') react JS错误:未捕获(承诺中)TypeError:无法读取未定义的属性 - react JS error: Uncaught (in promise) TypeError: Cannot read properties of undefined 未捕获的 TypeError:无法读取 Simple React 项目中未定义(读取“0”)的属性 - Uncaught TypeError: Cannot read properties of undefined (reading '0') in Simple React Project 'Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'writeFile')' 尝试使用 REACT 写入文件时出错 - 'Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'writeFile')' error while trying to write to a file with REACT 如何修复此错误:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“长度”) - how fix this error:Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length') Uncaught (in promise) TypeError: Cannot read properties of undefined (reading &#39;state&#39;) - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'state') Uncaught (in promise) TypeError: Cannot read properties of undefined (reading &#39;emailAddress&#39;) - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'emailAddress')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM