I get some data via an API call
getPosts(postRequest: PostRequest): void {
this._postService.getPosts(postRequest).subscribe(result => {
const postList = result as PostList
this.posts = postList.posts
});
}
the this.posts, contain an array of post objects. That in this case look like this:
{
id: 123
rating: {
datetime: 1496782640,
down:0,
hot:8061.5151856,
id:344,
up:2
}
}
Later I want to change the item
incrementVote(post: Post) {
post.rating.up++;
}
ERROR:
main-browser.min.js:2 ERROR TypeError: Cannot assign to read only property 'up' of object '[object Object]'
at PostListComponent.incrementVote (main-browser.min.js:34)
at PostListComponent.vote (main-browser.min.js:34)
at Object.handleEvent (main-browser.min.js:54)
at Object.handleEvent (main-browser.min.js:6)
at Object.handleEvent (main-browser.min.js:7)
at dispatchEvent (main-browser.min.js:5)
at main-browser.min.js:5
at HTMLButtonElement.<anonymous> (main-browser.min.js:21)
at ZoneDelegate.invokeTask (main-browser.min.js:67)
at Object.onInvokeTask (main-browser.min.js:3)
Why is the this.posts object read-only?
This suddenly happen seemingly without any changes to the code. I also have the same problem in another project, and then it works if I compile with --aot and not when I compile in dev mode.
How can I fix this? I need to be able to change values from the API. Why does this happen?
PS. The object is also used in the HTML, like so:
<div class="row" *ngFor="let post of posts; let postIndex = index">
<div class="col-md-12">
<app-post [post]="post" [fontSize]="30"></app-post>