Нow 使用 angular-in-memory-web-api 更新数据库中的数据(文件 data.service.ts)。在此代码中,我收到一条错误消息:“对‘所有者’ID 的请求与 item.id 不匹配”。同时,使用相同的链接删除工作_______________________________。
owner.component.ts
constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute,
private ownerService: OwnerService, private fb: FormBuilder) {
this.subscription = route.params.subscribe(params => this.params.owner_id = params.id);
console.log(this.params);
this.form = this.fb.group({
id: [this.params.owner_id, [Validators.required]],
FirstName: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(20)]],
}); }
ngOnInit(): void {................ }
submit() {
this.submitted = true;
const car: Car = {
number: this.form.value.number,
brand: this.form.value.brand,
model: this.form.value.model,
year: this.form.value.year,
};
const car1 = [car];
const owner: Owner = {
id: this.form.value.id,
FirstName: this.form.value.FirstName,
LastName: this.form.value.LastName,
MiddleName: this.form.value.MiddleName,
cars: this.owner.cars?.concat(car1)
};
this.ownerService.editOwner(owner).subscribe(() => this.router.navigate(['']));
}}
所有者服务.ts
editOwner(owner: Owner): Observable<Owner> {
console.log(owner.id);
return this.http.put<Owner>(this.ownersUrl + owner.id, owner, this.httpOptions).pipe(
catchError((error: HttpErrorResponse) => {
console.error(error);
return throwError(error);
}));}
数据服务.ts
import {Injectable} from '@angular/core';
import {InMemoryDbService} from 'angular-in-memory-web-api';
@Injectable({
providedIn: 'root'
})
export class DataService implements InMemoryDbService {
constructor() {
}
createDb() {
const owners = [
{
id: 1,
LastName: 'Seaman Cap',
FirstName: 'Seaman Cap',
MiddleName: 'Lorem ipsum',
cars: [{
number: '1063uy',
brand: 'BMW',
model: 'optima',
year: 2012,
},
{
number: '1060uy',
brand: 'kia',
model: 'optima',
year: 2010,
},
]
},]
return {owners};
}}