Javascript 返回带有map()的数组时出现未捕获错误

Javascript 返回带有map()的数组时出现未捕获错误,javascript,reactjs,fetch-api,Javascript,Reactjs,Fetch Api,我正在用React.js构建一个搜索引擎,在那里我可以使用GIPHY Gif的API查找GIPHY Gif。当我在搜索栏中键入一个单词时,会出现以下错误:Uncaught(in promise)TypeError:props.gifs.map不是函数 在礼品列表(SelectedList.js:19) 控制台日志返回一个数组,如下所示: import React from 'react'; import GifItem from './SelectedListItem'; const GifL

我正在用React.js构建一个搜索引擎,在那里我可以使用GIPHY Gif的API查找GIPHY Gif。当我在搜索栏中键入一个单词时,会出现以下错误:Uncaught(in promise)TypeError:props.gifs.map不是函数 在礼品列表(SelectedList.js:19)

控制台日志返回一个数组,如下所示:

import React from 'react';
import GifItem from './SelectedListItem';

const GifList = (props) => {
  console.log(props.gifs); // Logs Array in the console
  const gifItems = props.gifs.map((image) => { // <=======
    return <GifItem key={image.id} gif={image} />
   });

  return (
    <div className="gif-list">{gifItems}</div>
  );
};

export default GifList;
从“React”导入React;
从“/SelectedListItem”导入项目;
常量列表=(道具)=>{
console.log(props.gifs);//控制台中的日志数组
const gifItems=props.gifs.map((image)=>{//response.json())。然后((gifs)=>{
控制台日志(gifs);
控制台日志(gifs.长度);
这是我的国家({
吉布斯:吉布斯
});
});
};  
render(){
返回(
);
}
}
ReactDOM.render(,document.getElementById('root'));

感谢您的帮助!谢谢!:)

根据您的评论,
props.gifs
是一个对象,
props.gifs.data
是一个数组。所以你需要写作

const gifItems = props.gifs && props.gifs.data && props.gifs.data.map((image) => { 
    return <GifItem key={image.id} gif={image} />
});
const gifItems=props.gifs&&props.gifs.data&&props.gifs.data.map((图像)=>{
返回
});

根据您的评论,
props.gifs
是一个对象,
props.gifs.data
是一个数组。所以你需要写作

const gifItems = props.gifs && props.gifs.data && props.gifs.data.map((image) => { 
    return <GifItem key={image.id} gif={image} />
});
const gifItems=props.gifs&&props.gifs.data&&props.gifs.data.map((图像)=>{
返回
});

您确定这是一个数组吗?因为它告诉你它不是。它返回以下内容:
{data:Array(25),分页:{…},meta:{…}}数据:(25)[{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…}“5a799ce56f4d51616b4e7eaf”}分页:{total_count:6596,count:25,offset:0}{u____:Object
@AndyWell这是一个对象,不是数组。对象没有
.map()
props.gifs.data
可能就是你要找的。在
props
中放置
typeof
你确定它是数组吗?因为它告诉你它不是。它返回以下内容:
{data:array(25),分页:{…},meta:{…}数据:(25)[{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…}元:{状态:200,消息:“OK”,响应id:“5A79CE56F4D51616B4E7EAF”}分页:{总计数:6596,计数:25,偏移量:0}}}{
props.gifs.data
可能就是你想要的。在
props
Uncaught TypeError:无法读取未定义的属性“map”
@Shubham khatri提供数据可用性检查。检查更新。现在检查这个答案是否有效,但gifs已损坏@Shubham khatry怎么办您的意思是这样的中断:@Shubham Khatri
Uncaught TypeError:无法读取未定义的属性“map”
@Shubham Khatrip提供数据可用性检查。检查更新。现在检查此答案是否正常,但GIF已中断@Shubham Khatri您这样的中断是什么意思:@Shubham Khatri