ARTICLE AD BOX
I have an object model that has an array as one of its values. I'm trying to populate it but I get an error in my VS Code environment:
Type 'Promise<GenericViewModel[]>' is missing the following properties from type 'GenericViewModel': rating, reportingDate, comments, and 4 more.ts(2740)
I have searched and can't determine how to populate the property correctly. Everything I have seen online says that this is the correct way. I am using Visual Studio Code and building an Angular application.
My function and model are as follows:
async getRawData(){ let pList: PViewModel[] = []; let criteria = {storedProcedure: 'sp_Get_Data_For_ESM'}; await super.post('universal-sp-pass-thru/spPassThroughnoBody',criteria,'api-eprs'); pList = this.apiPostResponse.data; const scorecardData: ScorecardModel = {pcorecardModel: []}; // Process each pList.forEach((p) => { const pData: PScorecardModel = { pId: p.pId, OpenViewModel: [], }; pData.OpenViewModel[] = this.getGenericData(p.pId, p.reportingDate,p.OpenViewModel_ST,p.OpenViewModel_TABLE) scorecardData.pScorecardModel.push(pData); }); } async getGenericData(pId:string,reportingDate:Date | null, ised:string, tablename:string): Promise<GenericViewModel[]> { let GenericViewModel: GenericViewModel[] = []; let body = { pId: pId, reportingDate:reportingDate, ised: ised, tablename: tablename, }; let criteria = { storedProcedure: 'sp_GetScorecard', body: body, }; await super.post('sword-universal-sp-pass-thru/spPassThrough',criteria,'api-eprs'); GenericViewModel = this.apiPostResponse.data; return GenericViewModel; } /** Generic view‑model used in many collections */ export interface GenericViewModel { rating: string; reportingDate: Date | null; comments: string; results: string; rtg_date: string; phase: string; denominator: number; } export interface ProgramScorecardModel { pId: string; OpenViewModel: GenericViewModel[]; }I get the error on this line:
pData.OpenViewModel[] = this.getGenericData(p.pId, p.reportingDate,p.OpenViewModel_ST,p.OpenViewModel_TABLE)I have added the await as indicated in the comments. The error changes to:
Type 'GenericViewModel[]' is missing the following properties from type 'GenericViewModel'
I have tried modifying the GetGenericData output results to the following:
data = this.apiPostResponse.data; const GenericViewModel: GenericViewModel[] = data.map(p => ({ rating: p.rating, comments: p.comments, reportingDate: p.reportingDate, results: p.results, rtg_date:p.rtg_date, phase:p.phase, denominator:p.denominator, })); return GenericViewModel;The await super.post returns a Promise.
How do I properly populate the pData.OpenViewModel[]?
