0

i am looking for a way to get a query during a foreach with angular. I have a table like this:

players = [
{
  id: 'af948a0a-66be-44b1-af85-7ccd1a289df0',
  pseudo: 'DocEo.AG',
  age: '31',
  twitch: '',
  youtube: false,
  facebook: false,
  twitter: '',
  stats: [],

}];

I would like at the foreach, get the stats via the id of the person and push in my table, but for the moment without success. Here is my code for the moment that does not work.

ngOnInit() {
    this.players = this.teamService.players;

    this.statUser();  
}

statUser() {
    this.players.forEach(function(element) {
        console.log(element.id);
        this.getStats('element.id');
    });
}

getStats(idUser) {
    this.http.get(this.urlIdTest + idUser).subscribe((data) => {
        this.getStat = data;
    });
}

EDIT: I have a new problem, everything works fine but I have this error message in the console:http://prntscr.com/p2kvu8 My array: http://prntscr.com/p2kwpf

4
  • Why are you passing the string 'element.id' to getStats()? How about passing element instead (i.e. the user object), get the stats for that user, and update the user with the stats you obtained? Commented Aug 30, 2019 at 13:29
  • That's what I want to do, but I can not find the right syntax or the right way to do it ... Because if I try this.getStats (element.id); it does not work Commented Aug 30, 2019 at 13:34
  • Again, what you should pass is element, not element.id. You want to pass the user object, not its ID. Commented Aug 30, 2019 at 13:37
  • Do not do this. Making multiple http calls like this will not be good for your application's performance. Instead maybe modify your API to accept a a string of ids separated by a , or ; as your query params and use join(",") or join(";") to join the ids before you send the request. Commented Aug 30, 2019 at 13:41

2 Answers 2

1

You are passing id as 'element.id' (string) modify code like:

...
statUser() {
    this.players.forEach(function(element) {
        console.log(element.id);
        this.getStats(element.id);
    });
}
...

Update 1

You can optimize your code by passing complete object and then assign stats in it.

statUser() {
    this.players.forEach((player) => {
        this.getStats(player);
    });
}

getStats(player) {
    this.http.get(this.urlIdTest + player.id).subscribe((data) => {
        player.stats = data;
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Eolynas yes that's why I updated my answer you can send complete object and assign stats in it in subscriber.
Perfect it works .... I just had a problem with the synthaxe .... thank you very much EDIT: What is the difference between: this.players.forEach(function(element) { this.getStats(element.id); }); and statUser() { this.players.forEach((player) => { this.getStats(player); }); }
"thank you, on the other hand this.getStat does not return anything" reason within my answer
0

Just a little side note, please avoid vanilla functions within Angular Components. Always use Arrow-functions. Vanilla functions change the scope of this

this.players.forEach(function(element) {
        console.log(element.id);
        this.getStats('element.id'); // the this here
    });

this.players.forEach(element => {
        console.log(element.id);
        this.getStats('element.id'); // is not the same here
    });

2 Comments

I think you mean arrow functions, not Array-functions :). And really, this should be a comment...
Yeah thanks of course I mean arrow functions ;-) edited

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.