Javascript 无法映射react js中接收到的响应
我正在编写一个基于react-redux的web应用程序,但我无法映射从redux商店得到的响应。我总是得到一个错误,说地图不是一个函数。我可以通过控制台记录数据来查看数据,但在下一秒我使用map函数检索数据时,数据就会崩溃。这是回复,我无法映射任何数据 这是我为获得blockcount响应而创建的代码Javascript 无法映射react js中接收到的响应,javascript,reactjs,react-redux,react-router,Javascript,Reactjs,React Redux,React Router,我正在编写一个基于react-redux的web应用程序,但我无法映射从redux商店得到的响应。我总是得到一个错误,说地图不是一个函数。我可以通过控制台记录数据来查看数据,但在下一秒我使用map函数检索数据时,数据就会崩溃。这是回复,我无法映射任何数据 这是我为获得blockcount响应而创建的代码 import React,{useffect}来自“React”; 从'react redux'导入{connect}; 从“../Redux/Post-Redux/PostActionMe
import React,{useffect}来自“React”;
从'react redux'导入{connect};
从“../Redux/Post-Redux/PostActionMethods”导入{fetchPost};
导入“/PostBody.css”;
函数PostBody({postdata}){
console.log(postdata);
var blockcount=postdata.postchapter.map((键)=>{
return key.blockcount
})
console.log(blockcount);
return postdata.isLoading?(正在加载…)
postdata.error?({postdata.error}):
(
一些文本
)
}
常量mapStatetoProps=(状态)=>{
返回{
postdata:state.PostChapter
}
}
导出默认连接(MapStateTops)(PostBody)代码>是一种方法,但postdata.postchapter
是一种方法
你就不能用它吗
var blockcount = postdata.postchapter.blockCount;
而不是
var blockcount = postdata.postchapter.map((key)=>{
return key.blockcount
})
--编辑--
如果需要呈现postdata.postchapter.content
text
属性,例如
然后:
函数PostBody({postdata}){
console.log(postdata);
var content=postdata.postchapter.content?.map((项目、索引)=>{
返回{item.text}
})
return postdata.isLoading?(正在加载…)
postdata.error?({postdata.error}):
(
{content}
)
}
postdata.postchapter是数组吗?您正在尝试映射postdata.postchapter.content吗?postdata.postchapter
似乎是控制台日志中的一个对象。除非您真的想要映射postdata.postchapter.content
,否则没有任何可映射的内容。你想要呈现什么,postdata.postchapter.blockcount
?我想找到一种方式来呈现包含59个对象的内容响应,我该如何做?我能够做到。谢谢,但是我如何呈现包含59个对象的content对象中的所有数据呢?content
array看起来怎么样?显示console.log(postdata.postchapter.content[0])的输出。
我添加了图像,请检查它。答案很好,但是当我刷新页面时,我收到一个错误,说无法读取未定义的属性“map”。我是怎么过来的?我编辑了答案。注意内容之后的?
。现在能用了吗?
function PostBody({postdata}) {
console.log(postdata);
var content = postdata.postchapter.content?.map((item, index)=>{
return <p key={index}>{item.text}</p>
})
return postdata.isLoading ? (<h2>Loading...</h2>) :
postdata.error ? (<h2>{postdata.error}</h2>) :
(
<div>
{content}
</div>
)
}