我不明白如何在陣列中顯示所有電影。在控制臺中:
'index.jsx:14 GET https://api.themoviedb.org/3/movie/undefined?api_key=66eb3bde9cca0487f03e78b512b451e4 404
{success: false, status_code: 34, status_message: 'The resource you requested could not be found.'}'
我的代碼如下:
import axios from "axios";
import React, { useEffect, useState } from "react";
const Main = () => {
const [recipes, setRecipes] = useState([]);
useEffect(() => {
getRecipes()
},[])
const getRecipes = async (id) => {
const response = await fetch(
`https://api.themoviedb.org/3/movie/${id}?api_key=66eb3bde9cca0487f03e78b512b451e4`
);
const data = await response.json()
setRecipes(data.id)
console.log(data)
}
return(
<main></main>
)
}
export default Main;
uj5u.com熱心網友回復:
您沒有將 id 發送到 getRecipes 函式,因此它會導致錯誤,因為該 id 在您的函式中未定義。
useEffect(() => {
getRecipes("2") //Here you should pass the id
},[])
此外,您匯入了 axios 而不使用它。
const getRecipes = async (id) => {
const response = await axios.get(`https://api.themoviedb.org/3/movie/${id}?api_key=66eb3bde9cca0487f03e78b512b451e4`);
const data = response.data;
console.log(data);
}
uj5u.com熱心網友回復:
我假設您正在尋找所有電影串列,而不是電影資料。如果這就是您的意思,那么:-
就像在 tmdb 檔案中一樣,您可以發現電影、檔案。
import axios from "axios";
import React, { useEffect, useState } from "react";
const Main = () => {
const [recipes, setRecipes] = useState([]);
useEffect(() => {
getRecipes()
},[])
const getRecipes = async () => {
const response = await fetch(
`https://api.themoviedb.org/3/discover/movie?api_key=<your_api_key>`
);
const data = await response.json()
setRecipes(data.results) // `results` from the tmdb docs
console.log(data)
}
return(
<main></main>
)
}
export default Main;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/403749.html
標籤:
