我需要填充以下类型的文档对象。我正在使用Angular 7打字稿。我遇到错误,类型'void []'无法分配给类型'IDocumentDetails []
export interface IDocumentUpload {
fileDropEntry: NgxFileDropEntry;
name: string;
selectedDocumentItem: { 'Id': number; 'Name': string; }
selectedDate: Date;
}
export interface IDocumentDetails {
name: string;
file: any;
documentTypeId: number;
documentDate: Date;
}
export interface IDocuments {
managerStrategyId: number;
documentDetails: IDocumentDetails[];
}
documents: IDocuments;
public files: IDocumentUpload[] = [];
这行代码出现错误
const documents: IDocumentDetails[] = this.files.map(doc =>
代码
const documents: IDocumentDetails[] = this.files.map(doc => {
return
[{
file: doc.fileDropEntry.fileEntry,
documentTypeId: doc.selectedDocumentItem.Id,
name: doc.name,
documentDate: doc.selectedDate
}];
});
this.documents = {managerStrategyId : 0, documentDetails: null};
this.documents.managerStrategyId = this.ManagerStrategyId;
this.documents.documentDetails = documents;
答案 0 :(得分:0)
更新了答案,只是尝试是否有效?
const documents: IDocumentDetails[] = this.files.map((doc) => {
return {
file: doc.fileDropEntry.fileEntry,
documentTypeId: doc.selectedDocumentItem.Id,
name: doc.name,
documentDate: doc.selectedDate
};
});
this.documents = {managerStrategyId : 0, documentDetails: null};
this.documents.managerStrategyId = this.ManagerStrategyId;
this.documents.documentDetails = documents;
答案 1 :(得分:0)
由于您的return语句后没有方括号'(',因此您什么也不返回。 尝试将大括号与“返回”字词放在同一行。
Alos,就像@JSON Derolu所说的那样,您的.map函数需要返回一个对象,而不是数组,因此请删除方括号。
尝试此代码:
const documents: IDocumentDetails[] = this.files.map(doc => {
return { // notice just a curly bracket, and in the same line with 'return'
file: doc.fileDropEntry.fileEntry,
documentTypeId: doc.selectedDocumentItem.Id,
name: doc.name,
documentDate: doc.selectedDate
};
});
答案 2 :(得分:0)
您的const documents
类型为IDocumentDetails[]
,因此您需要在地图函数中返回类型为IDocumentDetails
的对象。
尝试一下。
const documents: IDocumentDetails[] = this.files.map((doc: IDocumentUpload): IDocumentDetails => {
return {
file: doc.fileDropEntry.fileEntry,
documentTypeId: doc.selectedDocumentItem.Id,
name: doc.name,
documentDate: doc.selectedDate
};
});
通过指定数据类型,我使您的代码具有更强的类型。这里map函数中的doc
参数的类型为IDocumentUpload
,函数的返回类型为IDocumentDetails
,即您在map函数中返回的对象。
答案 3 :(得分:0)
尝试这样发送您的回复:
const documents: IDocumentDetails[] = this.files.map(doc => {
return
[{
file: doc.fileDropEntry.fileEntry,
documentTypeId: doc.selectedDocumentItem.Id,
name: doc.name,
documentDate: doc.selectedDate
}] as IDocumentDetails[];
});
答案 4 :(得分:0)
检查其是否正常工作
替换此:
const documents: IDocumentDetails[] = this.files.map(doc => {
return
[{
file: doc.fileDropEntry.fileEntry,
documentTypeId: doc.selectedDocumentItem.Id,
name: doc.name,
documentDate: doc.selectedDate
}];
});
this.documents = {managerStrategyId : 0, documentDetails: null};
this.documents.managerStrategyId = this.ManagerStrategyId;
this.documents.documentDetails = documents;
与此:
const documents: IDocumentDetails[]=[]
this.documents = this.files.map(doc => {
return
[{
file: doc.fileDropEntry.fileEntry,
documentTypeId: doc.selectedDocumentItem.Id,
name: doc.name,
documentDate: doc.selectedDate
}];
});
this.documents = {managerStrategyId : 0, documentDetails: null};
this.documents.managerStrategyId = this.ManagerStrategyId;
this.documents.documentDetails = documents;