简体   繁体   中英

Property 'id' does not exist on type 'typeof Foods'

Looking at my code below, i am trying to delete clients from the my datatable using the checkboxes. When i tick a checkbox, i see in the console that the object's properties have been fetched from the databasae. i am trying to delete by id but the terminal raises this error "Property 'id' does not exist on type 'typeof Clients'". I haven't found any solution yet after my search. Any help?

//Http service

@Injectable()

 export class HttpService {

  constructor(private http:Http) {

  }



  deleteFood(food) {
    return this.http.delete('http://localhost:9000/api/v1/food/${id}')
      .map((response:Response) => response.json())
  }
}

//table

 <tbody>
    <tr *ngFor="let food of Foods; let i = index">
      <td><input #{{food.id}} [(ngModel)]="food.selected" type="checkbox" (change)="checkbox(food)"></td>

      <td>{{client.type}}</td>
      <td>{{client.location}}</td>

    </tr>

  </tbody>
</table>

<button type="button" (click)="deleteFood()">Delete</button>

//component

  export class FoodComponent {

  Foods : Foods[] = []; 



  constructor(private httpService: HttpService, private router: Router){



   selectedFood = Foods;

   deleteFood(){
    this.httpService.deleteFood(this.selectedFood.id)
     .subscribe(data =>{
       console.log(data)
     })
  }

  checkbox(food){
    food.selected = (food.selected) ? false: true;
    this.selectedFood = food;
    console.log(this.selectedFood);
  }
}

//Food.ts

  export class Food{

  constructor(
    public type:string,
    public location: string,

 ){}
}

In your component you are assigning the Clients type to your activeClient property instead of defining its type. And your Clients class has no static property id , that's why the compiler complains when you access this.activeClient.id .

If you change:

activeClient = Clients;

to:

activeClient: Clients;

You should not have the problem anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM