简体   繁体   English

无法将可选属性设置为 null

[英]cannot set optional property to null

I have a model class, where I defined an optional property like this:我有一个 model class,我在其中定义了一个像这样的可选属性:

 export class Workflow { constructor( public id: number, public started: Date, public documentId: number, public document: Document, public status: WorkflowStatus, public workflowEvents: WorkflowEvent[], public startingUserId: number, public statusValue?: string, ) {} }

I have a pipe too, which transforms the status propery to a string:我也有一个 pipe ,它将状态属性转换为字符串:

 import { Pipe, PipeTransform } from '@angular/core'; import { WorkflowStatus } from '../models/workflowstatus.enum'; @Pipe({ name: "status" }) export class WorkflowStatusPipe implements PipeTransform { public transform(value: WorkflowStatus): string { switch (value) { case WorkflowStatus.ForwardedToApprover: return "TOV"; case WorkflowStatus.ReadedByApprover: return "OLV"; case WorkflowStatus.Commented: return "MEGJ"; case WorkflowStatus.Approved: return "JÓV"; case WorkflowStatus.ForwardedToManager: return "MAN"; } } }

Then I have document objects.然后我有文档对象。 To each document we can have a workflow.对于每个文档,我们可以有一个工作流程。 I get the documents via api:我通过 api 获得文件:

 this.documentRepo.getDocuments().subscribe(documents => { documents.forEach(function (document) { document.workflow.statusValue = new WorkflowStatusPipe().transform(document.workflow?.status); }); this.documents = documents; });

As you see, I'm trying to transform the status value to a string, if there is a workflow.如您所见,如果有工作流,我正在尝试将状态值转换为字符串。 But I get in the console this error: 'Cannot set property 'statusValue' of null'.但我在控制台中收到此错误:'无法设置属性'statusValue' of null'。

I don't really unterstand, why, because I set the statusValue property to an optional property.我真的不明白,为什么,因为我将statusValue属性设置为可选属性。

How should I modify the code?我应该如何修改代码?

EDIT:编辑:

this is a document object from the result of getDocuments:这是来自 getDocuments 的结果的文档 object:

在此处输入图像描述

Another weird thing: if I just simply assign a nonsense string to the statusValue like this, I get the same error:另一个奇怪的事情:如果我只是像这样简单地将一个无意义的字符串分配给 statusValue,我会得到同样的错误:

 this.documentRepo.getDocuments().subscribe(documents => { documents.forEach(function (document) { document.workflow.statusValue = "x";//new WorkflowStatusPipe().transform(document.workflow?.status); }); this.documents = documents; });

I think you just need to handle the case where property document.workflow does not exist or is null.我认为您只需要处理属性document.workflow不存在或者是 null 的情况。 Set a default workflow value to documents with no workflow or simply skip the processing in documents with no workflow, something like为没有工作流程的文档设置默认工作流程值,或者只是跳过没有工作流程的文档中的处理,例如

this.documentRepo.getDocuments().subscribe(documents => {
  documents.forEach(function (document) {
    if (document.workflow) {
      document.workflow['statusValue'] = new WorkflowStatusPipe().transform(document.workflow?.status);
    }
  });
  this.documents = documents;
});

Notice also if document.workflow.status is not of the values considered in your switch statement you will get undefined as result.另请注意,如果document.workflow.status不是您的 switch 语句中考虑的值,您将得到未定义的结果。 Depending on what you expect there you may want to add a default case to return a different value.根据您对那里的期望,您可能希望添加默认情况以返回不同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法设置 null 的属性值 - uncaught TypeError: Cannot set property value of null TypeError:无法将属性'setHash'设置为null - TypeError: Cannot set property 'setHash' of null 角度2:无法将属性“位置”设置为null - Angular 2: Cannot set property 'location' of null 为什么将分配给属性的结果错误“无法将属性设置为null” - Why assigning to property resulting error “cannot set property of null” 地理位置:错误类型错误:无法将属性“lat”设置为空 - Geolocation: ERROR TypeError: Cannot set property 'lat' of null 无法将属性'src'设置为null Angular-打字稿错误 - Cannot set property 'src' of null Angular - Typescript error 错误:未捕获(在承诺中):TypeError:无法将属性'isAdmin'设置为null - Error: Uncaught (in promise): TypeError: Cannot set property 'isAdmin' of null build dev failed:无法将属性'fileSystem'设置为null - build dev failed: Cannot set property 'fileSystem' of null 使用它时,无法在angular-2中设置null属性? - Cannot set property of null in angular-2 when using this? 如何解决:无法在Angular 6中设置null错误的属性“ innerHTML”? - How to fix : Cannot set property 'innerHTML' of null error in Angular 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM