next.js Map对象但提供属性'title'的对象在型别'never'上不存在

8ehkhllq  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(139)

我尝试获取bloomberg api,但是当我Mapresponse对象以获取title时,它给了我以下错误Property 'title' does not exist on type 'never'.

<div>
            {loading? <h1>Loading...</h1> :null}
            {data.map((item) => ( 
              <div className="mt-10 text-center">
                <h1 className="text-2xl">{item.title  }</h1>
                <a href={item.longURL}>Visit</a>
                </div>))
                }
          </div>```

full code - https://github.com/Anurag30112003/FinApp/blob/main/pages/index.tsx
esyap4oy

esyap4oy1#

即使您还没有获取API,您Map也会呈现,因此数据是空数组,还没有任何元素您需要添加条件,仅当数据不为空时才呈现您还需要为Map添加键

<div>
  {loading? <h1>Loading...</h1> :null}
  {data && data.length > 0 && data.map((item) => ( 
     <div className="mt-10 text-center" key={'your unique key in here'}>
        <h1 className="text-2xl">{item.title  }</h1>
        <a href={item.longURL}>Visit</a>
     </div>
    ))
   }
</div>

相关问题