-1
How do I make a forEach
on the return of a subscribe?
this.service.obterTodos()
.subscribe(result => {
result.forEach(element => {
this.usuComponent.push(element);
});
});
Service:
obterTodos(): Observable<Usuario[]> {
return this.httpClient.get<Usuario[]>("URL");
}
Error:
ERROR TypeError: result.forEach is not a function
Result:
What additional information I need to post for you to help me?
result
is the value you hoped it would be. It’s a array?– Woss
You’re right, sorry for posting as image... I put my result in the post
– LeoHenrique
try result.result.foreach
– Eduardo Vargas
@Eduardovargas, does not allow, is underlined in red
– LeoHenrique
Leo, why in the result print did you obfuscate a piece? It’s sensitive data?
– Woss
See also the result of
console.log(typeof result)
– Woss
@Andersoncarloswoss is the real name of my object
– LeoHenrique
Why obfuscate the object’s name? And the result of
typeof
?– Woss
To avoid confusion, I put in the user example, then they would question what is that other name there, you are not going wrong, just to avoid this kind of comment... typeof result is Object
– LeoHenrique
There you are then. Yours
result
is an object, not a array. Should be a array? Do you have control over the resource that was requested on the server? It returns JSON?– Woss
Let’s go continue this discussion in chat.
– LeoHenrique
Possibly related: What is an Array-Like?
– Icaro Martins