3

I can't get the addReservation function to return any value.

in reservations.page.ts it returns an empty array when I log to the console.

userReservations: Reservation[];
private resSubscription: Subscription;
constructor(private resService: ReservationsService) { }

      ngOnInit() {
        this.resSubscription =
        this.resService.reservations.subscribe(reservations => {
          console.log('reservations...', reservations);
          this.userReservations = reservations;
        });
      }

below is reservations.service.ts I can't get the reservations in the return statement to log any value so I'm guessing the issue may be there.

export class ReservationsService {
  private _reservations = new BehaviorSubject<Reservation[]>([]);

  constructor(private authentication: AuthenticationService) { }

  // return the parking space reservations
  get reservations() {
    return this._reservations.asObservable();
  }

  // create a reservation
  addReservation(
    parkingSpaceId: string,
    parkingSpaceTitle: string,
    url: string,
    firstName: string,
    lastName: string,
    reservedDayCount: number,
    reservedFrom: Date,
    reservedTo: Date
  ) {
    const newReservation = new Reservation(
      Math.random().toString(),
      parkingSpaceId,
      this.authentication.userId,
      parkingSpaceTitle,
      url,
      firstName,
      lastName,
      reservedDayCount,
      reservedFrom,
      reservedTo
    );
    return this.reservations.pipe(
      take(1),
      tap(reservations => {
        console.log('return reservations...', reservations);
        this._reservations.next(reservations.concat(newReservation));
      })
    );
  }  
}
3
  • I would have to do this.resService.reservations in reservations.page.ts right? using this.reservations I get property does not exist. Commented Apr 27, 2019 at 2:56
  • can you show where you use 'addReservation'? Commented Apr 27, 2019 at 2:58
  • something like this: dev.to/avatsaev/… Commented Apr 27, 2019 at 3:34

1 Answer 1

3

The problem is in your addReservation function.

The way you have it, the tap will only execute if addReservation is subscribed to... which is wrong because that will result in an infinite loop.

The following link shows how to use Rxjs exclusively to manage state within an Angular service, without having to store any local copies of the data:

https://dev.to/avatsaev/simple-state-management-in-angular-with-only-services-and-rxjs-41p8

This is how you would apply the pattern to your ReservationsService:

export class ReservationsService {
   get reservations(): Reservation[] {
        return this._reservations.getValue();
   }

   private set reservations(val: Reservation[]) {
     this._reservations.next(val);
   }

   private _reservations = new BehaviorSubject<Reservation[]>([]);

   constructor(private authentication: AuthenticationService) { }

  // return the parking space reservations
  get reservations$() {
    return this._reservations.asObservable();
  }

  // create a reservation
  addReservation(
    parkingSpaceId: string,
    parkingSpaceTitle: string,
    url: string,
    firstName: string,
    lastName: string,
    reservedDayCount: number,
    reservedFrom: Date,
    reservedTo: Date
  ) {
    const newReservation = new Reservation(
      Math.random().toString(),
      parkingSpaceId,
      this.authentication.userId,
      parkingSpaceTitle,
      url,
      firstName,
      lastName,
      reservedDayCount,
      reservedFrom,
      reservedTo
    );
    this.reservations = [
      ...this.reservations, 
      newReservation
    ];

  }  
}
Sign up to request clarification or add additional context in comments.

3 Comments

if I do this._reservations.next(this.reservations.concat(newReservation)); I get Property 'concat' does not exist on type 'Observable<Reservation[]>'
right... you need to store a local reference to your array
can you please give an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.