[英]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.