0

I am currently trying to program an app getting info from the Mock API http://jsonplaceholder.typicode.com/ I did create an array with all the user data. The problem is that I am not able to get the data from all the users separately using a button.
My code is:

app.component

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import "rxjs";
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { User } from './user';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  readonly ROOT_URL = 'http://jsonplaceholder.typicode.com/';

  users : Observable <User[]>;



  constructor(private http: HttpClient) {}

  getUsers(users){
    this.users = this.http.get<User[]>(this.ROOT_URL + 'users' )
  }

  getUser(){
   this.users = this.http.get<User[]>(this.ROOT_URL + 'users/1')
 }
}

app.component.html

<h1>User List</h1>
<button (click) = "getUsers()">User List</button>
<div *ngFor= "let user of users | async ">
  {{ user | json }}
  <button (click) = "getUser()">Login</button>
</div>

app.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { UserComponent } from './user/user.component';
import { UserDetailsComponent } from './user/user-details/user-details.component';
import { UserListComponent } from './user/user-list/user-list.component';

@NgModule({
  declarations: [
    AppComponent,
    UserComponent,
    UserDetailsComponent,
    UserListComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

user.ts

export interface User{
    id: number;
    userid:number;
    title: string;
    body: string;

}
Vega
  • 27,856
  • 27
  • 95
  • 103
  • What is your expectation? You are populating the same variable `users` and that is an object when you get just one user, so it's not iterable. I'd assume you'd want to store it in a different variable like `currentUser` or something? – AT82 Oct 23 '17 at 11:38
  • Im a rookie to angular to be honest. I tried to add another variable but then the login button does absolutely nothing, plus there is no error report from the console. – antonis charalambus Oct 23 '17 at 12:41
  • A bit unclear what you want to do, but based on the code and question the root problem is like it's marked in the duplicate, you are trying to iterate an object, which is not possible. So I will mark this as a duplicate, but I'll throw in a demo for you to look at :) https://stackblitz.com/edit/angular-4kdcif?file=app%2Fapp.component.ts Please inform if you feel this is not a duplicate tho :) – AT82 Oct 23 '17 at 12:49

0 Answers0