简体   繁体   English

在打字稿中赋值后未定义的值

[英]Undefined value after assignment in Typescript

I'm trying to subscribe to a service in angular to get my customer's branding info through their Id.我正在尝试订阅 Angular 服务,以通过他们的 ID 获取客户的品牌信息。

Firstly, I load all customer data:首先,我加载所有客户数据:

 this.subscription = this.burstService.getBurst().subscribe(async(response) => { if (response) { if (response.length > 0) { var survey = response[0]; this.storageService.set("customer", JSON.stringify(survey)); this.sessionJson.Id = survey.Id; this.sessionJson.Name = survey.Name; this.sessionJson.Email = survey.Email; this.sessionJson.SubDomainName = survey.SubDomainName; this.sessionJson.BusinessType = survey.BusinessType; this.sessionJson.Address = survey.Address; this.sessionJson.Phone = survey.Phone; this.sessionJson.Status = survey.Status; this.sessionJson.CreatedBy = survey.CreatedBy; this.sessionJson.CreationDate = survey.CreationDate; this.sessionJson.ModifiedBy = survey.ModifiedBy; this.sessionJson.ModificationDate = survey.ModificationDate; this.customerId = survey.Id; } } });

Then I try to subcribe to another api to get their branding data but the customer's Id seems undefined although when I assign its values in the getBurst bethod it equals to 1.然后我尝试订阅另一个 api 以获取他们的品牌数据,但客户的 Id 似乎未定义,尽管当我在getBurst bethod 中分配它的值时它等于 1。

 this.burstService.getBranding(this.sessionJson.Id!).subscribe(async(response) => { if (response) { if (response.length > 0) { var customerBranding = response[0]; this.storageService.set("customerBranding", JSON.stringify(customerBranding)); } } });

错误信息

Here are my observables in case its necessary:如果有必要,这是我的观察结果:

 getBurst(): Observable < CustomerModel[] > { var url = `https://localhost:44397/api/Customer/` + splitUrl[1]; console.log(url); return this.httpClient.get < CustomerModel[] > (url, this.httpOptions); } getBranding(id: number): Observable < BrandingModel[] > { var url = `https://localhost:44397/api/Branding/` + id return this.httpClient.get < BrandingModel[] > (url, this.httpOptions); }

And lastly my class attributes:最后是我的类属性:

 export class CategoryComponent implements OnInit { public survey ? : SurveyModel; private subscription: Subscription | undefined; private sessionJson: CustomerModel = {}; private customerId: any; constructor(private storageService: StorageService, private burstService: BurstService, private sessionService: SessionService, private ip: IpserviceService, private router: Router, private activateRoute: ActivatedRoute) {} ngOnInit(): void { this.loadData(); } ...

What's happening here is that the properties of the data model sessionJson are in PascalCase and they should be in camelCase .这里发生的是数据模型sessionJson的属性在PascalCase中,它们应该在camelCase中。 Check variable name casing types for more info.检查变量名称大小写类型以获取更多信息。

For this project, the api is written in ASP.Net Core Web API, which uses C# as the programming language.对于这个项目,API 是用 ASP.Net Core Web API 编写的,它使用 C# 作为编程语言。

It is standard for C# to use PascalCase for naming variables, but once the api sends a response to the client, the JSON with the response info has properties in camelCase, which is the standard for JavaScript.使用PascalCase命名变量是 C# 的标准,但是一旦 api 向客户端发送响应,带有响应信息的 JSON 就具有 camelCase 中的属性,这是 JavaScript 的标准。

The model is undefined after the assignment because it is looking for a Pascal-cased property in the JSON, which doesn't exist, for example:分配后模型未定义,因为它正在 JSON 中查找不存在的 Pascal 大小写属性,例如:

this.sessionJson.Email = survey.Email;

After storing the response object in survey, all its properties are in camelCase, so in reality if you want to access survey 's objet e-mail property you should do:将响应对象存储在调查中后,它的所有属性都在 camelCase 中,所以实际上如果你想访问survey的对象电子邮件属性,你应该这样做:

this.sessionJson.email = survey.email;

Notice that I also changes sessionJson 's email property.请注意,我还更改了sessionJson的 email 属性。 I also suggest naming all your property values in camelCase for consistency in JavaScript in this case, since that's the casing type the api is serving.在这种情况下,我还建议以camelCase命名您的所有属性值,以便在 JavaScript 中保持一致性,因为这是 api 所服务的大小写类型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM