使用地图填充对象时出现角度错误

时间:2019-07-04 16:34:48

标签: angular

我需要填充以下类型的文档对象。我正在使用Angular 7打字稿。我不确定是什么问题?

grep -B 1 "status" file
awk '/status/ {print s"\n"$0} {s=$0}' file
name = any_name
status = Wait

我遇到错误

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[] = [];

代码

Type '{ managerStrategyId: any; 
     documentDetails: { file: FileSystemEntry; documentTypeId: number; name: string; 
     documentDate: Date; }; }[]' is missing the following properties from 
     type 'IDocuments': managerStrategyId, documentDetails

根据Tom Tran的建议更新了JSOn

尽管错误消失了,但我的json的结构却是

    this.documents = this.files.map(doc => {
            return {
                managerStrategyId: this.ManagerStrategyId,
                documentDetails: {
                    file: doc.fileDropEntry.fileEntry,
                    documentTypeId: doc.selectedDocumentItem.Id,
                    name: doc.name,
                    documentDate: doc.selectedDate
                }

            };
        });

2 个答案:

答案 0 :(得分:0)

您的类型IDocuments(即documents: IDocuments的类型)包含一个IDocumentDetails的数组。

    export interface IDocuments {
        managerStrategyId: number;
        documentDetails: IDocumentDetails[];
    }

因此,您需要一些如何将documentDetails映射为数组而不是单个对象的方法。

因此,(假设我不确定这里的业务规则,只是尝试解决该错误),解决方案是:

  • 更正映射以将documentDetails制成数组:
    this.documents = this.files.map(doc => {
        return {
            managerStrategyId: this.ManagerStrategyId,
            documentDetails: [{
                file: doc.fileDropEntry.fileEntry,
                documentTypeId: doc.selectedDocumentItem.Id,
                name: doc.name,
                documentDate: doc.selectedDate
            }]
        };
    });

(请注意方括号:documentDetails: [{ ... }]

或:

  • documentDetails的类型定义更改为单个对象:
    export interface IDocuments {
        managerStrategyId: number;
        documentDetails: IDocumentDetails;
    }

已更新:

又一个问题,您正在从IDocumentUpload(files: IDocumentUpload[])数组进行映射,因此可能还需要将documents定义为数组:

documents: IDocuments[];

(您不能将数组映射到单个对象中-导致对数组中的每个对象进行映射定位并产生新的映射对象-1-1关系)

已更新为仅具有一个managerStrategyId和整个文档阵列:

简单点

保留您的定义作为缩写:

 export interface IDocuments {
        managerStrategyId: number;
        documentDetails: IDocumentDetails[];
    }

<<第一步:将文件(数组)映射到文档(数组)

-  let documents = this.files.map(doc => {
            return 
                 {
                    file: doc.fileDropEntry.fileEntry,
                    documentTypeId: doc.selectedDocumentItem.Id,
                    name: doc.name,
                    documentDate: doc.selectedDate
                };
        });

<<第二步:

this.documents.managerStrategyId =  this.managerStrategyId; and set
this.documents.documentDetails = documents (mapped array above)

答案 1 :(得分:0)

您的this.files是一个数组,因此您无法映射到这样的对象。试试这个看看是否可行

 this.documents =  this.files.reduce((obj: IDocuments, item) => {
          return {
            ...obj,
            documentDetails: obj.documentDetails,
            managerStrategyId: 1
          }
        }, {})  as IDocuments