An older project of mine was originally created in Angular 7. Recently, I felt inspired and decided to update it to version 13. During development, everything seemed to work fine. However, when I attempted to compile the project using ng serve
to deploy it on my server along with the API, I encountered an error:
ERROR TypeError: "count_categories" is read-only
Dictionary dictionary.ts:8
set dictionaries.service.ts:34
getAll dictionaries.service.ts:84
(..)
The issue seems to be related to "Strict Mode", as the code runs smoothly on serve but crashes during AOT compilation.
The problematic section in the code can be found in the constructor of the Dictionary class:
public constructor(init?: any) {
Object.assign(this, init); // The error is here
}
id: number;
name: string;
language: string;
dateCreated: Date;
dateRevision: Date;
@Optional() changes: boolean;
@Optional() novel: Novel[];
// tslint:disable-next-line: variable-name
@Optional() count_categories: [{count: number}];
countCategories(): number {
if (this.count_categories[0]) {
return this.count_categories[0].count;
}
return 0;
}
This error arises from assigning a value to the optional array "count_categories" using Object.assign which leads to the read-only issue during AOT compilation. Despite attempts to disable Strict Mode, the problem persists. Any suggestions for resolving this issue or alternative approaches would be greatly appreciated.
The data provided by the API looks like this:
{
"id": 1,
"name": "asd",
"language": "English",
"dateCreated": "2019-05-28T01:29:13.000000Z",
"dateRevision": "2022-01-14T17:56:34.000000Z",
"count_categories": [
{
"idDictionary": 1,
"count": 14
}
]
}
The challenge lies in finding a solution to overcome the read-only error during AOT compilation in Angular. Your insights and expertise are welcome in identifying a workaround or resolution for this perplexing issue.
As someone relatively unfamiliar with Angular, I seek guidance on how best to address this technical hurdle and leverage the platform's capabilities effectively.