[英]"Uncaught TypeError: posts.map is not a function" , How to solve this error?
So I was fetching data from my local drupal instance in react js.所以我在 react js 中从本地 drupal 实例中获取数据。 I pass the data from drupal with json api module (Which send the data in json format).
我使用 json api 模块从 drupal 传递数据(以 json 格式发送数据)。 The following image is the data in form of json api.
下图是json api形式的数据。
And following is how i am fetching the api and mapping it in on front end.以下是我如何获取 api 并将其映射到前端。
import React, { useEffect, useState } from "react";
import Slider from "react-slick";
function Homeslidersection() {
const settings = {
dots: false,
arrows: false,
infinite: true,
speed: 500,
autoplay: false,
slidesToShow: 1,
slidesToScroll: 1,
};
const [posts, setPosts] = useState([]);
useEffect(() => {
async function loadPosts() {
const response = await fetch(
"http://localhost/drupalreact/jsonapi/node/banner?include=field_banner_image"
);
if (!response.ok) {
// oups! something went wrong
return;
}
const posts = await response.json();
setPosts(posts);
}
loadPosts();
}, []);
return (
<>
<div className="home-slider-section">
<div className="homesliderwrapper">
<div className="home-slider-folder">
<Slider {...settings}>
{posts.map((post,index) => (
<div className="home-slider-blog-box-section">
<div className="home-slider-image">
<img src="" />
</div>
<div className="home-slider-text-folder1">
<h2>Looking</h2>
<span>Flexible Workspace In Your City!</span>
<p>
Our global network of workspaces enable you to work wherever
you need to be.
</p>
<a href="#" className="Read-More">
Read More
</a>
</div>
</div>
))}
</Slider>
</div>
</div>
</div>
</>
);
}
export default Homeslidersection;
I know the error Uncaught TypeError: posts.map is not a function is due my data coming from drupal json api is not in array format.我知道错误Uncaught TypeError: posts.map is not a function是因为我的数据来自 drupal json api 不是数组格式。
Like if see the image, the whole data is {[]}
not in array [...]
format就像看到图像一样,整个数据是
{[]}
不是数组[...]
格式
How do i solve this error?我该如何解决这个错误? Because i cannot change anything from my drupal end, so how can solve this error from my front end code in react js?
因为我不能从我的 drupal 端改变任何东西,所以如何从我的前端代码中解决这个错误?
data is inside the data
property.数据在
data
属性内。
setPosts(posts.data);
As the data received from the API is having multiple properties ( jsonapi
, data
), and you are storing the whole response in your state, that's why it was throwing the error.由于从 API 接收的数据具有多个属性(
jsonapi
、 data
),并且您将整个响应存储在您的状态中,这就是它抛出错误的原因。
Two solutions:两种解决方案:
data
array in your state ( setPosts(posts.data)
)data
数组存储在您的状态中( setPosts(posts.data)
)jsonapi
object somewhere, you can change your map function ( posts.data.map(...)
)jsonapi
对象,你可以改变你的地图功能( posts.data.map(...)
) One reason is actual data is in response.data, so you should setState(response.data) as explained in above answer.原因之一是实际数据在 response.data 中,因此您应该按照上述答案中的说明设置状态(响应.data)。 The second reason is your posts is empty because setState() is asynchronous.
第二个原因是你的帖子是空的,因为 setState() 是异步的。 It means the posts in jsx runs before posts is populated with data.
这意味着 jsx 中的帖子在帖子填充数据之前运行。 You can solve it by using two ways.
您可以使用两种方法解决它。 1).
1). Conditional rendering
条件渲染
{ posts.length && posts.map(----)}
2). 2). Null Check(just put? before.map)
Null Check(刚刚放?before.map)
posts?.map(--)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.