我正在使用Directus 6和Angular 6,因此我正在编写一个与Directus CMS中的API交互的Web应用程序。触发update方法时,我对活动有点困惑,因为PATCH方法(Directus允许进行编辑)上请求的有效负载格式正确,并显示该行已设置为要更新,但是一旦收到响应,则数据不会使用新设置返回。
config.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { map } from 'rxjs/operators';
import { Config } from './config';
import { Observable } from 'rxjs';
@Injectable()
export class ConfigService {
private configURL = 'https:someurl/api/1.1/tables/config/rows';
data: any = {};
config: Config[];
id = 1;
// ^ there is only one row in this table, which is why I have id set to 1//
constructor(private http: Http) { }
getConfig() {
return this.http.get(this.configURL).pipe(map((response: any) =>
response.json().data));
}
updateConfig(id, config: Config) {
return this.http.patch(this.configURL + '/' + id,
JSON.stringify(config)).pipe(map((res:any) => res.json().data));
}
}
formation.component.ts
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { HometeamService } from '../hometeam/hometeam.service';
import { HomeTeam } from '../hometeam/hometeam';
import { ConfigService } from '../config.service';
import { Config } from '../config';
import { ShapesService } from './shapes.service';
import { Shapes } from './shapes';
@Component({
selector: 'app-formation',
templateUrl: './formation.component.html',
styleUrls: ['./formation.component.scss']
})
export class FormationComponent implements OnInit {
config: Observable<Config[]>;
shapes: Shapes[];
hometeam: Observable<HomeTeam[]>;
submitted = false;
id = 1;
selectedFormation: any;
constructor(
private hometeamService: HometeamService,
private configService: ConfigService,
private shapesService: ShapesService,
) {}
ngOnInit() {
this.getHomeTeam();
this.getConfig();
this.getShapes();
}
getHomeTeam(): void {
this.hometeamService.getHomeTeam().subscribe(hometeam => this.hometeam = hometeam);
}
getConfig(): void {
this.configService.getConfig().subscribe(config => {
this.config = config;
console.log('oldConfig:', this.config);
});
}
getShapes(): void {
this.shapesService.getShapeTypes().subscribe(shapes => {
this.shapes = shapes;
});
}
updateFormation() {
this.config[0].home_team_formation.data = this.selectedFormation;
console.log('updatedConfig:', this.config);
this.configService.updateConfig(this.id, this.config[0]).subscribe(
config => {
this.getConfig();
console.log('newConfig:', config);
return true;
},
error => {
console.error('Error updating Settings', error);
return Observable.throw(error);
}
);
}
}
因此,请求的有效负载正在使用新信息发送新数组,但是响应随后显示了旧数据。我与Directus保持联系,但可能与我的低调编码技能有关。
任何帮助将不胜感激。