简体   繁体   English

fetch 给了我一个完整的数组,但是当我尝试访问它时它是空的

[英]fetch gives me a full array but it is empty when i try to access it

I get an object from a Symfony rest API.我从 Symfony rest ZDB974238714CA8DE634A7CE1D083A14 得到 object。 This object has a property "shooting" which is an array and this array is full when I console.log it but when i try to access it, it is empty这个 object 有一个属性“正在拍摄”,它是一个数组,当我 console.log 时这个数组是满的,但是当我尝试访问它时,它是空的

This is my fetch request这是我的获取请求

const getProjectsAvailable = async () => {
    const data = await fetch(
      `${process.env.GATSBY_CORE_URI}/api/dashboard/supplier/projects/available`,
      {
        headers: {
          [`X-Auth-Token`]: `${token}`,
          [`Accept`]: `application/json`,
        },
      }
    );

    return data;
  };

Here is the project object that i get back from fetch request这是我从获取请求中返回的项目 object

0: {id: 258, name: "Project26-1", reference: "A6568", isOfferValidated: null, source: "dashboard", …}

It has a shooting key which contains an array and it is not empty它有一个包含数组且不为空的射击键

shootings: Array(1)
   0:
   addressCity: "Paris"
   addressCountry: {id: 76}

But when i set this object to my component state, all values stay the same except the shooting key which becomes an empty array但是当我将这个 object 设置为我的组件 state 时,所有值都保持不变,除了射击键变成一个空数组

const [projects, setProjects] = useState([]);

useEffect(() => {
  getProjectsAvailable().then(res =>
    res.json().then(data => {
      setProjects(data);
    })
  );
}, []);

I have no idea why does it act like that.我不知道为什么会这样。

Thanks in advance提前致谢


EDIT:编辑:

For example, the first line with console.log gives me the response object with a full shooting array while the second one sets it to my state but shooting array is empty例如,console.log 的第一行给了我响应 object 一个完整的射击阵列,而第二行将它设置为我的 state 但射击阵列是空的

  useEffect(() => {
    getProjectsAvailable().then(response => console.log(response));
    getProjectsAvailable().then(response => setProjects(response));
  }, []);

Ok it is my bad.好吧,这是我的错。 Somewhere else in the code, there was a.split() on the shooting property which mutates the array so the props changed and shooting array got empty在代码的其他地方,拍摄属性上有一个 a.split(),它改变了数组,所以道具改变了,拍摄数组变空了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试通过索引访问数组元素时,它给了我未定义的 - when i try to access array element by index it gives me undefined 我从 API 获取数组,但是当我尝试在表中显示它时它显示空数组(noob ReactJs) - I GET array from API but when i try show it in the table it show me empty array (noob ReactJs) Fetch:我在 DevTools 中看到了响应,但 Fetch 仍然给我一个 GET 的“Access-Control-Allow-Origin”问题 - Fetch: I see the response in the DevTools but Fetch still gives me an "Access-Control-Allow-Origin" issue for GET 为什么当我尝试访问它的数组时,传递给第二个 function 的 object(包含一个数组)会给出一个空数组? - Why does the object (containing an array) passed to second function, give an empty array when I try to access its array? firestore:当我尝试按日期获取数据时,数组为空 - firestore: array empty when i try to get data by date 尝试访问空的JavaScript数组,而不会出现错误 - Try to access empty JavaScript array without error 为什么当我用空的购物车登录时,.map出现错误? - Why when I login with the empty shopping cart it gives me an error with .map? 当我尝试访问javascript数组时,正在填充未定义的值 - Undefined value is being populated when i try to access javascript array 我正在使用 jQuery 在传单地图中插入地标。 但是当我尝试通过单击链接插入地标时,它给了我错误 - I'm inserting placemarks in leaflet map with jQuery. But when I try to insert the placemarks by clicking a link it gives me error 无法显示来自 MongoDB 的内容,给我一个空数组 [] - Can't display things from MongoDB, gives me empty array []
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM