I'm having trouble returning data that I received from an httpClient get method in angular. I'm getting all of the objects from the DB and I implement an if statement to return an array with some of the objects.
All of the following code is in a service.ts file
getRecipes():Observable<Recipe[]> {
return this.http.get(this.baseURL + 'recipes') as Observable<Recipe[]>
}
getRecipesByTag(tag:string):Recipe[]|void{
let possibleRecipesArr:Recipe[] = []
this.getRecipes().subscribe((data:Recipe[])=>{
data.forEach((rcp:Recipe)=>{
if(rcp.tags.includes(tag)){
possibleRecipesArr.push(rcp)
}
})
console.log(possibleRecipesArr);
return possibleRecipesArr
})
}
for testing purposes I'm calling the function from the constructor
console.log(this.getRecipesByTag('quick'));
the following is the console output enter image description here
in the console you can see undefined and then the returned object. when I comment out "console.log(possibleRecipesArr);" all I can see in the console is undefined -> therefore we can assume that the undefined came from the function return and not the console.log(possibleRecipesArr)
- why is my return statement returning undefined when clearly the variable "possibleRecipesArr" contains the desired object? (as I can see in the console)
- why is the return statement being printed out to the console before the console.log(possibleRecipesArr)?
p.s I tried to add casting to the return statement but it didn't make a difference
return possibleRecipesArr as Recipe[]