【发布时间】:2022-01-04 17:09:41
【问题描述】:
我不明白如何在一个数组中显示所有电影。在控制台中:
'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;
【问题讨论】:
-
id 没有传递给
getRecipes -
只需看看 URL:
/movie/undefined?!另请注意,您现在需要轮换您的 API 密钥(出于这个原因,您不应将其包含在客户端代码中),因为您已公开共享它。 -
嘿.. 不要把你的有效 api 密钥放在这里!只需放一个样本或将其替换为
API_KEY