I'm building a react app. I tried to make an axios call to the database, and it should return an array of objects with the image id, and then I make another axios call to get all the image urls and add them into the original array. The data object returned from getSrc is updated with the correct url property, but when I set the data object to sessionStorage, it has no url property. I really don't understand why it behaves like this, pls help thank you so much.
const [items, setItems] = useState([]);
const getSrc = (datas) => {
return axios.all(
datas.map((data) => {
axios
.get(
`https://database/${data.imageID}`,
{}
)
.then((res) => {
if (res.status != 200 || !res.data) {
throw res;
} else {
let src = "data:image/jpeg;base64,";
src += res.data;
data.url = src;
}
})
.catch((err) => {
console.log(err);
});
})
);
};
useEffect(() => {
axios
.get("https://server/getPage", {})
.then((res) => {
if (res.status != 200 || !res.data) {
throw res;
} else {
const data = res.data;
getSrc(data).then(() => {
setItems(data);
console.log(data);//updated
sessionStorage.setItem("items", JSON.stringify(data));
console.log(JSON.parse(sessionStorage.getItem("items")));// original object
});
}
})
.catch((err) => {
console.log(err);
});
}, []);