I'm currently struggling with the following issue:
I have a function call like this:
foo = this.initializeFoo(id, array); // console.log() says: undefined
And the function:
export function initializeFoo(id, array) {
axios.get(API_URL + '/route/' + id)
.then(response => {
let copyPayload = [...response.data.foo];
let copyArray = [...array];
// Some operations
copyArray = copyArray.filter(x => {
let exists = false;
copyPayload.forEach(payload => {
if (x.id === payload.id) {
x["newAttribute"] = id;
exists = true;
}
});
return exists
});
console.log("Returning copyArray", copyArray); // Displays the proper data
return copyArray;
})
.catch(error => {
this.setState({loaded: false});
console.log(error);
})
}
The question is: Why is the console.log()
undefined
? I guess it has something to do with the way I return
the array within the axios
call, but I can't think of another way to do it.
I also don't want to use setState
within the function, since I call a few initialize functions and I'd rather want to use one setState
after I got all the data initialized.
Thanks in advance!
UPDATE
Can I do it this way:
foo["blub"] = this.initializeFoo(id, array).then(result => {
return result;
});