I have an object called "values" that contains a property "images" that is an array of strings. When I console.log "values", I do see the "images" property as an array of strings. However, when I console log values.images, the result is empty. How could this be?
Here's the react-redux code:
I'm calling the confirm method.
const confirm = () => {
console.log("form values in confirm");
console.log(formValues);
let images = [];
for (let i = 0; i < formValues.images.length; i++) {
var reader = new FileReader();
reader.onload = function(e) {
images.push(e.target.result);
console.log('images base 64');
console.log(images);
};
reader.readAsDataURL(formValues.images[i].file);
}
formValues["images"] = images;
console.log("values after adding images");
console.log(formValues);
upload(formValues, history);
};
This snippet is from the action creator:
export const upload = (values, history) => async dispatch => {
console.log("images in uploadProduct action");
console.log("the whole object");
console.log(values);
console.log("values.images");
console.log(values.images);
};
-- edit --
The comments point out that the console.logs are lazily evaluated which is why I'm seeing the property when printing the whole object. Good to know. However, the goal is to in fact get the values.images property before further execution. How can I do that?