简体   繁体   English

“Uncaught TypeError: posts.map is not a function”,如何解决这个错误?

[英]"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 接收的数据具有多个属性( jsonapidata ),并且您将整个响应存储在您的状态中,这就是它抛出错误的原因。

Two solutions:两种解决方案:

  • Either only store the data array in your state ( setPosts(posts.data) )要么仅将data数组存储在您的状态中( setPosts(posts.data)
  • Or, if you want to use 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.

相关问题 为什么我有这个错误“Uncaught TypeError: posts.map is not a function” - Why I have this error "Uncaught TypeError: posts.map is not a function" 未处理的运行时错误类型错误:posts.map 不是 function - Unhandled Runtime Error TypeError: posts.map is not a function REACTJS:类型错误:posts.map 不是 function - REACTJS: TypeError: posts.map is not a function 删除帖子时出现“posts.map 不是函数”错误 - I get “posts.map is not a function” ERROR when I delete a post 如何解决“未捕获的类型错误:$ 不是函数”? - How to Solve “Uncaught TypeError: $ is not a function”? 我正在尝试将 JSON 数据的值存储在来自 servlet 的数组中,但它显示帖子。map 不是 function - I am trying to store value of JSON data in an array from servlet but it shows posts.map is not function 未捕获的错误:TypeError:data.map 不是函数 - Uncaught error: TypeError: data.map is not a function 错误未捕获类型错误:numbers.map 不是 function - Error Uncaught TypeError: numbers.map is not a function React Native应用程序可在iOS上运行,但在Android posts上出现错误。未定义地图 - React Native app works on iOS but comes up with error on Android posts.map not defined 如何解决错误:TypeError: AppliNom.map is not a function - How to solve the error : TypeError: AppliNom.map is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM