0

As all questions look like they are outdatet i'm asking this Question.

First of all I need to mention that I just started Angular 4 days ago, so please explain everything like you would to your child ( or sth. that comes near that haha).

I currently try to retrieve a JSON from a Flask Server.

It has this format.

{"id":[1,2,3],"name":["Test 1","Test 2","Test 3"]}

The examples will be missing the Imports because it worked properly when testing and now the data should come from a Server.

When I try to Iterate using *ngFor the following error shows up.

Error: "Error trying to diff 'Test 1,Test 2,Test 3'. Only arrays and iterables are allowed"

The HTML for this is:

<ul>
  <li *ngFor="let test of tests">
          <a routerLink="/detail/{{test.id}}">
          <span>{{test.id}}</span> {{test.name}}
          </a>
  </li>
</ul>

In my test.service.ts it looks the following.

...
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class TestService {

private testUrl = 'http://my.server/tests';

constructor(
  private http: HttpClient
  ) { }

getTests(): Observable<Test[]> {
        return this.http.get<Test[]>(this.testUrl);
        }
...

And in my tests.component.ts like this:

...
@Component({
  selector: 'app-tests',
  templateUrl: './tests.component.html',
  styleUrls: ['./tests.component.css']
})
export class TestsComponent implements OnInit {
  tests: Test[];

  getTests(): void {
        this.testService.getTests()
                .subscribe(tests => this.tests = tests);

  }


  constructor(private testService: TestService) { }

  ngOnInit(): void {
  this.getTests();
  }

}

After trying to figure it out for 2 hours I don't know how any further.

If something is missing tell me.

Cheers!

  • are you sure that `this.tests` gets array result ? – micronyks Jul 26 '20 at 14:38
  • because error says `this.tests` gets object which is => `{"id":[1,2,3],"name":["Test 1","Test 2","Test 3"]}` and its not an array so you can't bind it to `ngFor`. So you get the error mentioned above. – micronyks Jul 26 '20 at 14:40
  • Like i mentioned. It should get the JSON, beforehand it recieved an array, after i switched to the Server it doesn't. The problem could be that it somehow 'screws it up' (which is then my error) but i simply don't know how to handle this. @micronyks do you know how to fix it? –  Jul 26 '20 at 14:41
  • Its not clear what you want to explain but make sure that `this.tests` must get an array. – micronyks Jul 26 '20 at 14:50
  • Sorry haha my english is not that good. But well I got a good answer below. Thanks for your help too –  Jul 26 '20 at 14:51

2 Answers2

0
let test of tests

This is the root of your problem. This is not an iterables or array. tests.id and tests.name these are arrays. It is very clear by looking at the response from server. ngFor will work on tests.id or tests.name. tests is just an object.

abhay tripathi
  • 3,547
  • 4
  • 20
  • 25
  • so you mean i should iterate over tests.id and tests.name got it. But how do i get them so that iterating ovet tests would be possible again? Maybe im just stubborn but knowing more helps more. Thanks for the good answer though :) –  Jul 26 '20 at 14:48
  • ...so that iterating ovet tests would be possible again? I didn't got your point. Nevertheless, i would suggest you to dig further in your code and if it still dont work , than we will see. – abhay tripathi Jul 26 '20 at 14:53
  • sometimes very obvious seem too difficult to understand. We just have to give some patience – abhay tripathi Jul 26 '20 at 14:54
  • Do 2 Things : 1. In your testService.getTests().subscribe(tests => console.log(tests)); Check if you are getting the results as expected or not. – abhay tripathi Jul 26 '20 at 15:03
  • So just trying it out and corrected it to `let id of tests.id` now i get ` ERROR in src/app/tests/tests.component.html:3:34 - error TS2339: Property Test[]'. 3
  • ~~ src/app/tests/tests.component.ts:9:16 9 templateUrl: './tests.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component TestsComponent.` Now im confused.
  • –  Jul 26 '20 at 15:05
  • 2. Server takes some time to send response. If you are accessing the tests variable before server sends data, than still you will get error. – abhay tripathi Jul 26 '20 at 15:05
  • console.log doesn't seem to display anything. Mhm. Yes the server could take a while but i can't even build it when i want to add the for loop. –  Jul 26 '20 at 15:13
  • So im still stuck at `Property 'id' does not exist on type 'Test[]'. ` –  Jul 26 '20 at 15:33