我不知道如何在数组中显示所有的电影。在控制台中:
'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;发布于 2022-01-04 17:43:00
我想你是在寻找所有的电影列表,而不是电影数据。如果这就是你的意思,那么:-
和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;发布于 2022-01-04 17:27:47
您没有向getRecipes函数发送id,因此它将导致错误,因为id在您的函数中未定义。
useEffect(() => {
getRecipes("2") //Here you should pass the id
},[])此外,您没有使用axios就导入了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);
}https://stackoverflow.com/questions/70582559
复制相似问题