0

I'm 'studying' Angular from a course-book. The example in book is not done by observable, but I have made my example with Json-server and a json file. I would like to update the rating value of the City object within a observable.

I have a city model/object:

export class City {
  constructor(
    public id: number,
    public name: string,
    public province: string,
    public rating: number,
    public highlights?: string[]
  ) { }
}

I have in my controller defined a variable: currentCityJsonServer$: Observable<City>;

When I click on a button the function updateRating is called, which should modify the rating value, with 1 or -1, of the city object:

export class AppComponent implements OnInit {
    
    public currentCityJsonServer$: Observable<City>;
    ...
    updateRating(rating: number): void {
        console.log("This is called from the detail screeen by the function rate via -EventEmitter - met het num:  ", rating);
    
        console.log("before update");
        this.currentCityJsonServer$
        .subscribe(
          data => {
            console.log("..val =", data);
            console.log("..val-name city:", data.name, ' --- rating', data.rating);
          },
          error => console.log("in error", error),
          () => console.log(" observable is now completed.")
        );
        
        console.log("Execute update by adding the rating value");
        this.currentCityJsonServer$.pipe(
          map(currentCityJsonServer => ({...currentCityJsonServer, rating: currentCityJsonServer.rating + rating})),
          tap(currentCityJsonServer => console.log("display the new city rating: ", currentCityJsonServer)) // will display the new city 
        );
        //See if the value has been changed 
        console.log("After update");
        this.currentCityJsonServer$
        .subscribe(
          data => {
            console.log("..val =", data);
            console.log("..specific value val-rating:", data.rating);
          },
          error => console.log("in error", error),
          () => console.log(" observable is now completed.")
        );
    
        console.log("end functie updateRating");
      }
    ...
    }
}

After the update value is still zero.

My question is how can I update/change the value of rating property of the City object with an observable?

user2363969
  • 105
  • 1
  • 10
  • You should use Subject instead of Observable. – Julian W. Jan 23 '22 at 15:30
  • 1
    Does this answer your question? [Typescript Angular - Observable: how to change its value?](https://stackoverflow.com/questions/42512502/typescript-angular-observable-how-to-change-its-value) – Julian W. Jan 23 '22 at 15:30
  • I've been googling a lot but you mean that update example what I have with map, is not possible? I found this on Stackoverflow and if I look at it, it looks 'logical' to me so I expected that is road to go. I have never used Subject, but I've been googling on it but it is not clear to me. I found also that I could use 'behaviourSubject'. Do you have a simple example? – user2363969 Jan 23 '22 at 22:01

0 Answers0