Within my Angular application, I have defined an object structure like this:
export class Article {
id: number;
author: number;
title: string;
content: string;
date: Moment;
readingTime: number;
draft: boolean;
constructor(obj: Partial<Article>) {
Object.assign(this, obj);
this.date = moment(this.date);
}
}
The issue arises with the property "author:number", which is meant to represent an object but only utilizes the ID when making HTTP calls to post articles. Upon receiving articles, all values within the object except for the ID are set to null. However, I need this ID to be associated specifically with the "author" property, instead of replacing it entirely.
I attempted to address this problem by trying to access the ID as follows: article.author.id (but encountered an error stating that "id" is not recognized as a property of the "number" type).
updateArticle(): void {
this.register(this.articleService.getArticles()
.pipe(tap(articles => {
articles.map(article => {
if(typeof article.author === 'object') {
return {...article , author: article.author.id};
}
})
}))
.subscribe(articles => this.articles = articles));
}