I am new in angular, i want to delete user by clicking on button . showing error when i try to delete a row.
DELETE: http://localhost:3000/User/undefined 404 not found
Please correct my mistake.
This is my user.component.html
table class="table table-hover">
<tr>
<th>SELECT</th>
<th>ID</th>
<th>NAME</th>
<th>ADDRESS</th>
</tr>
<tr *ngFor="let data of allUsers">
<td><input type="radio" name="id" [value]="allUsers.id" ></td>
<td>{{data.id}}</td>
<td>{{data.name}}</td>
<td>{{data.address}}</td>
</tr>
</table>
<div class="buttonClass">
<input type="button" class="btn btn-success" value="Add User"/>
<input type="button" class="btn btn-danger" (click)="deleteUser(allUsers.id)" value="Delete User"/>
</div>
This is my user.component.ts
import { Component, OnInit } from '@angular/core';
import { UserService } from './userService/user.service';
import { User } from '../../model/user.model';
@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
constructor(
private userService : UserService
) { }
ngOnInit() {
this.getallUser();
}
allUsers: User[];
getallUser(){
this.userService.getAllUsers().subscribe(
(data)=> {this.allUsers=data;},
(error)=> {console.log(error);},
()=> {console.log("All Users Recieved");}
);
}
deleteUser(id: number){
alert("In Delete")
this.userService.deleteUsers(id).subscribe(
(data)=> {this.getallUser();},
);
}
}
and This is my user.service.ts
deleteUsers(id): Observable<User>{
return this.http.delete<User>("http://localhost:3000/User"+"/"+id)
}
Edited to add:
here is my user.model.ts
export class User {
id: number;
name: string;
address: string;
}
and this is db.json file which contains const data
{
"User": [
{
"id": 1,
"name": "Shiva",
"address": "pune"
},
{
"id": 2,
"name": "mark",
"address": "mumbai"
},
{
"id": 3,
"name": "rinku",
"address": "solapur"
}
]
}
Thanks in advance.