![](/img/trans.png)
[英]Why does using an oscillator in the Web Audio API always leak memory in Chrome? bug?
[英]why genID() method in In-memory-web-api does not generate an ID?
我使用 angular-in-memory-web-api 來模擬數據服務器。 當我發布新的 model 以添加到數據庫時,由於屬性 id 出現錯誤,我認為服務中的 genId() 方法不起作用。 我的 model 是:
export class WorkHouse implements WorkHouseInterface{ id: number = 0; title: string = ''; code: string = ''; manager: string = ''; startDate: Date = new Date(); endDate: Date = new Date(); evalUnit: string = ''; }
而且,InMemoryDataService 是:
@Injectable({ providedIn: 'root' }) export class InMemoryDataService implements InMemoryDbService{ createDb() { const workHouses = [{id:1, title: 'work1', code: '523', manager: 'xyz', startDate: new Date(), endDate:new Date(), evalUnit: '1'}, {id:2, title: 'work2', code: '345', manager: 'abc', startDate: new Date(), endDate:new Date(), evalUnit: '2'}]; return {workHouses}; } genId(workHouses: WorkHouse[]): number { return workHouses.length > 0? Math.max(...workHouses.map(workHouse => workHouse.id)) + 1: 0; } }
因此,當我通過屬性 id 以外的表單填寫以下新 model 的屬性時:
this.newWorkHouse = {id:0, title:'',code:'',manager:'',startDate:new Date(''),endDate:new Date(''), evalUnit:''};
並通過調用 WorkHouse.service 中的 addWorkHouse 方法發布:
/** POST: add a new workHouse to the server */ addWorkHouse(workHouse: WorkHouse): Observable<WorkHouse> { return this.http.post<WorkHouse>(this.workHouseUrl, workHouse, this.httpOptions).pipe( tap((newWorkHouse: WorkHouse) => this.log(`added workHouse w/ id=${newWorkHouse.id}`)), catchError(this.handleError<WorkHouse>('addWorkHouse')) ); }
通過以下代碼:
this.workHouseService.addWorkHouse(this.newWorkHouse).subscribe(workHouse => { this.workHouses.push(workHouse)});
將屬性 id 定義為任何能夠初始 null 或定義。
export class WorkHouse implements WorkHouseInterface{ id: any = null; title: string = ''; code: string = ''; manager: string = ''; startDate: Date = new Date(); endDate: Date = new Date(); evalUnit: string = ''; }
在表格中,初始化 newWorkHouse:
this.newWorkHouse = {id:null, title:'',code:'',manager:'',startDate:new Date(''),endDate:new Date(''), evalUnit:''};
或在將 newWorkHose 發布到服務器之前,將 null 分配給其 id 屬性:
this.newWorkHouse.id = null;
考慮in-memory-web-api后端服務器post方法中的if (item.id == undefined)
語句調用genId(...)
方法生成id:
// Create entity // Can update an existing entity too if post409 is false. protected post({ collection, collectionName, headers, id, req, resourceUrl, url }: RequestInfo): ResponseOptions { const item = this.clone(this.getJsonBody(req)); // tslint:disable-next-line:triple-equals if (item.id == undefined) { try { item.id = id || this.genId(collection, collectionName); } catch (err) { const emsg: string = err.message || ''; if (/id type is non-numeric/.test(emsg)) { return this.createErrorResponseOptions(url, STATUS.UNPROCESSABLE_ENTRY, emsg); } else { console.error(err); return this.createErrorResponseOptions(url, STATUS.INTERNAL_SERVER_ERROR, `Failed to generate new id for '${collectionName}'`); } } } //......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.