Upon further examination, it appears that my initial assessment was correct. You are indicating a child of an object within an observable (regardless of your familiarity with observables) (observables may sometimes take precedence and occur before the hydration of any other variable).
Therefore, I recommend taking certain precautions to prevent potential issues: dividing tasks accordingly.
In addition, here's a helpful "hack" that I routinely employ to ensure smooth progress in my development process and gain a clearer understanding of where errors may be occurring rather than just encountering "undefined":
this.category['slug'];
While I personally do not typically go to this extent, it is permissible to use:
this['category']['slug'];
The distinction lies in the fact that this approach will reveal the undefined instead of halting at a JavaScript level.
It seems evident to me that verifying this.category.slug
during its evaluation ensures that it is not in an uninitialized state at that particular moment:
var myFunctionToLogSlug = () => { //You could pass this.category.slug as an argument here, but
//it is equivalent to calling it within the function body because your
//context (this) remains constant, hence the reference and timeframe also remain consistent.
console.log("is this.category.slug empty? : ", this.category.slug);
return this.category.slug;
};
this.WooCommerce.getAsync(
"products?filter[category]=" + myFunctionToLogSlug()).then(data =>{
console.log(JSON.parse(data.body));
});
Once you have confirmed that this.category.slug
is indeed undefined when called, you can either adjust the timing of this.WooCommerce.getAsync
to ensure this.category.slug
is defined or move the initialization of this.category.slug
inside the getAsync
.
I trust that this explanation proves beneficial! :)