I'm creating a dynamic array in the computed: part of my .vue file. This array has a set of urls I each want to call (in a repeated component), to get articles. These articles vary, so on page render the computed array is made, and when that's done I want to call the urls the moment my component is made.
computed: {
calcTheArray() {
// make the actual array
// basically, I grab a (computed! so can't use that in async either)
// array with months that have passed before today
// I create an array with combined urls, that incorporate those month names
// no other dependencies exist
return array;
}
},
then:
async asyncData({ error, app, }) {
try {
// lots of things happen here, another api call, some parsing etc.
const promises = [];
this.calcTheArray.forEach((value) => {
promises.push(app.$axios.$get(`${value}`, { useCache: true }));
});
} catch (e) {
console.log(e);
return error({ statusCode: 500, message: e.message });
}
},
The error I'm getting is: Cannot read property 'calcTheArray' of undefined
And I sort of understand why that is, as the async function is... well asynchronous, so my data doesn't exist yet? I think? But then how would I go about getting the dynamically created urls into my axios call?
What I've tried so far, is seeing whether the this.calcTheArray was the culprit and trying different ways to use the calculated array (vm.calcTheArray, just calcTheArray for example) I've also tried to define my the array in the data() { part
data() {
return {
calcTheArray: [],
};
},
But then I (obviously) end up with a Duplicated key 'calcTheArray'.
I'm at a loss. Can anyone give me a pointer?
thisinsideasyncDatabecause it is called before initiating the component.'. Without knowing whatcalcTheArraydepends on it is difficult to advise further.calcTheArrayonly depends on todays date (or its month to be more precise). For all past months from a starting point until this month, I have a different API call to make. I dynamically build those from an array of month names. That's pretty much allcalcTheArraydoes. I now do understand I why I can't use thecalcTheArrayin the async, but it's still unclear to me what I should do instead to get the calls working...calcTheArrayinto yourasyncDatafunction?