Reactjs 如何修复错误:未处理的拒绝(TypeError):meetups.map不是函数
我试图从我的api中获取数据,然后使用match参数及其工作(我检查过) 但当我绘制地图时,会出现这种错误,我不知道为什么: × 未处理的拒绝(TypeError):meetups.map不是一个函数Reactjs 如何修复错误:未处理的拒绝(TypeError):meetups.map不是函数,reactjs,react-hooks,Reactjs,React Hooks,我试图从我的api中获取数据,然后使用match参数及其工作(我检查过) 但当我绘制地图时,会出现这种错误,我不知道为什么: × 未处理的拒绝(TypeError):meetups.map不是一个函数 export default function Details({ match }) { const [meetups, setMeetups] = useState([]); useEffect(() => { async function loadMeetups() {
export default function Details({ match }) {
const [meetups, setMeetups] = useState([]);
useEffect(() => {
async function loadMeetups() {
const response = await api.get(`list/${match.params.id}`);
setMeetups(response.data);
}
loadMeetups();
}, [match.params.id]);
return (
<Container>
<Content>
{meetups.map(meetup => (
<>
<header>
<p>{meetup.title}</p>
<aside>
<button type="button">Editar</button>
<button type="button">Cancelar</button>
</aside>
</header>
<Meetup>
<div>
<img
src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRCbLCtAN0pPHM3cEtNR0tEpFf6r6AIHOUMjOnAVl2srJO-5lQP"
alt=""
/>
</div>
<article>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a type specimen
book.
</article>
<footer>
<p>SOME DATE</p>
<p>SOMEWHERE</p>
</footer>
</Meetup>
</>
))}
</Content>
</Container>
导出默认函数详细信息({match}){
const[meetups,setMeetups]=useState([]);
useffect(()=>{
异步函数loadMeetups(){
const response=wait api.get(`list/${match.params.id}`);
setMeetups(response.data);
}
loadMeetups();
},[match.params.id]);
返回(
{meetup.map(meetup=>(
{meetup.title}
编辑
取消者
Lorem Ipsum只是印刷和排版行业的虚拟文本
Ipsum自16世纪以来一直是行业标准的虚拟文本,当时
一位不知名的印刷工拿起一个铅字厨房,把它拼凑成一个铅字样本
书。
某个日期
某处
))}
提前感谢您要了解让我们重新创建错误:
var x = {}
console.log(x.map) // undefined
x.map() // Uncaught TypeError: x.map is not a function ...
我怀疑,如果您添加这样的console.log语句,您会发现问题:
async function loadMeetups() {
const response = await api.get(`list/${match.params.id}`);
console.log(response, response.data)
setMeetups(response.data);
}
要避免此错误,请验证response.data是否为数组
async function loadMeetups() {
const response = await api.get(`list/${match.params.id}`);
// if response or response.data is undefined, set data to empty array
const { data = [] } = response || {};
// verify response.data is an array
const isArray = Array.isArray(data)
isArray && setMeetups(data);
}
要理解此错误,请执行以下操作:
var x = {}
console.log(x.map) // undefined
x.map() // Uncaught TypeError: x.map is not a function ...
我怀疑,如果您添加这样的console.log语句,您会发现问题:
async function loadMeetups() {
const response = await api.get(`list/${match.params.id}`);
console.log(response, response.data)
setMeetups(response.data);
}
要避免此错误,请验证response.data是否为数组
async function loadMeetups() {
const response = await api.get(`list/${match.params.id}`);
// if response or response.data is undefined, set data to empty array
const { data = [] } = response || {};
// verify response.data is an array
const isArray = Array.isArray(data)
isArray && setMeetups(data);
}
您是否记录了
响应。数据
,并检查了它是否是数组,而不是对象或其他东西?请将您的问题标题设置为有意义的内容。它应该足够清晰和描述性,以供将来扫描搜索结果列表的读者使用。您当前的标题没有意义。使用清晰、描述性的标题lso可以帮助您更快地获得答案,因为能够回答的人可以看到答案的内容,而您当前的标题没有提供。谢谢。您是否记录了响应。数据,并检查它是否是数组,而不是对象或其他东西?请将您的问题标题设置为有意义的内容。它应该清晰且具有描述性h对未来浏览搜索结果列表的读者有用。您当前的标题毫无意义。使用清晰、描述性的标题也有助于您更快地获得答案,因为能够回答的人可以看到它的内容,而您当前的标题没有提供。谢谢。