Javascript 使用ReactJs映射获取的对象时出错 问题

Javascript 使用ReactJs映射获取的对象时出错 问题,javascript,reactjs,contentful,Javascript,Reactjs,Contentful,我想从contentflow获取文章,但这是我遇到的错误 TypeError: Cannot read property 'id' of undefined (anonymous function) E:/Project-VidM/client/src/components/Blog/index.js:21 18 | 19 | const Articles = article.map((entry) => { 20 | return( > 21 |

我想从contentflow获取文章,但这是我遇到的错误

TypeError: Cannot read property 'id' of undefined
(anonymous function)
E:/Project-VidM/client/src/components/Blog/index.js:21
  18 | 
  19 |   const Articles = article.map((entry) => {
  20 |       return(
> 21 |        <div className="entry" key={entry.sys.id}>
     | ^  22 |        <h1>{entry.fields.title}</h1>
  23 |        <img src={entry.fields.featuredImage.sys} />
  24 |        {entry.fields.body.data}
View compiled
TypeError:无法读取未定义的属性“id”
(匿名函数)
E:/Project VidM/client/src/components/Blog/index.js:21
18 | 
19 | const Articles=article.map((条目)=>{
20 |返回(
> 21 |        
|^22{entry.fields.title}
23 |        
24 |{entry.fields.body.data}
视图编译
这是我的代码
import React,{useState,useffect}来自“React”
将*从“contentful”导入为contentful;
常量BlogComponent=()=>{
const[article,setArticle]=useState([]);
const client=contentful.createClient({
空格:“xxxxxxxx”,
accessToken:'xxxxxxxxxxxxxxxxxxxx'
});
useffect(()=>{
client.getEntries()
。然后((res)=>{
固定物品([res.items]);
})
}, [])
const Articles=article.map((条目)=>{
返回(
{entry.fields.title}
{entry.fields.body.data}
)
}) 
返回(
{条款}
)
}
导出默认博客组件
当我在
res.items
上执行console.log时,我得到以下输出:


如何映射特定字段?

在console.log
res.items
中是一个数组,您将它放入另一个数组中


它应该是
setArticle(res.items)
而不是
setArticle([res.items])

谢谢,但这样做会给我带来以下错误:```对象作为react子对象无效(找到:具有键的对象{})。如果要呈现子项集合,请改用数组。`` entry.fields.body.data中的内容是什么?看起来它是一个对象,而不是字符串或数字@SubhankarChowdhury
import React, { useState, useEffect} from 'react'
import * as contentful from 'contentful';

const BlogComponent = () => {
    const [article, setArticle] = useState([]);

    const client = contentful.createClient({
    space: 'xxxxxxxx' ,
    accessToken: 'xxxxxxxxxxxxxxxxxxxxxxxx'
    });
    
    useEffect(() => {
       client.getEntries()
            .then((res) => {
                setArticle([res.items]);
            })
    }, [])

   const Articles = article.map((entry) => {
       return(
        <div className="entry" key={entry.sys.id}>
        <h1>{entry.fields.title}</h1>
        <img src={entry.fields.featuredImage} />
        {entry.fields.body.data}
        </div>
       )
    }) 
    return (
       <div>
           {Articles}
       </div>
    )
}

export default BlogComponent