1

I have an array of objects called Hobby. I am able to set the array of objects when the component first loads. But whenever I go to update that array of objects, I can't.

This is my component that lists an array of Hobbies. Hobby-View-Component. When I ever I make a call to the service component the hobbies[] array never gets updated thus returning a null value in my console.log method.

      export class HobbyViewComponent implements OnInit {
  hobbyRoomObj = HobbyRoomObj;
  hobbiesRooms: HobbyRoomObj[];
  ngOnInit() {
    this.data.currentMessage.subscribe(message => this.message = message);
    this.getHobbies();
  }

  getHobbies()
  {
    console.log("hit2");
    this.data.getHobbiesById().subscribe(data => this.hobbiesRooms = data);
    //console.log(this.hobbiesRooms.length.toString());
  }

The url is called and makes valid requests in this Service class next. I know what is being returned works. As mentioned before, this method works in ngOnIt.

export class MasterHobbydata {
private messageSource = new BehaviorSubject<string>("1");
  currentMessage = this.messageSource.asObservable();

  changeMessage(message: string) {
    this.messageSource.next(message);
    this.hobbyListByIdUrl += message;
  }

  //Returns a full list of current hobbies hosted
  getHobbiesById(): Observable<HobbyRoomObj[]> {
    console.log(this.hobbyListByIdUrl);
    return this.http.get<HobbyRoomObj[]>(this.hobbyListByIdUrl)
      .pipe(map((response) => {
        return response;
      }
      )).pipe(catchError(this.handleError('getHobbiesById', []))
      );
  }

This next component uses a onClick() method to tell the previous component to update. hbv is of type Hobby-View-Component.

export class HobbiesComponent implements OnInit {


  onSelect(hobby: hobbyObj): void {
    this.selectedHobby = hobby;
    this.masterhobbydata.changeMessage(this.selectedHobby.HobbyName);
    this.hbv.getHobbies();
  }

I hope that i focused on the problem correctly here.

This is my html for the Hobby-View-Component:

<ul>
  <li *ngFor="let hobby of hobbiesRooms">
      <span class="badge">{{hobby.hname}}</span>
  </li>
</ul>
<p>
  Message: {{message}}
</p>

//////////~~~~~~~!!!! huzzah !!!!~~~~~~~\\\\\\\\\\\\

Updated the Service class by creating a subject of type HobbyRoomObj[] and creating an observable variable. Also, using the Subject method next, I added the requested json object to the Subject of HobbyRooms[].

export class MasterHobbydata {

  private hobbies = new Subject<HobbyRoomObj[]>();
  newHobbies = this.hobbies.asObservable();

  //Returns a full list of current hobbies hosted
  getHobbiesById(): Observable<HobbyRoomObj[]> {

    console.log(this.hobbyListByIdUrl);
    return this.http.get<HobbyRoomObj[]>(this.hobbyListByIdUrl)
      .pipe(map((response) => {
        this.hobbies.next(response);
        return response;
      }
      )).pipe(catchError(this.handleError('getHobbiesById', []))
      );
  }

}

In Hobby-View-Component.ts I subscribed to the newHobbies Observable of the Service Component inside ngOnInIt. Then whenever I click on a new hobby, I subscribe the GetHobbiesById and update the list.

  ngOnInit() {
    this.data.currentMessage.subscribe(message => this.message = message);
    console.log("hit2");
    this.data.newHobbies.subscribe(
      data => {this.hobbiesRooms = data;
      });
  }

  getHobbies()
  {

    this.data.getHobbiesById().subscribe(
      data => {this.hobbiesRooms = data;
      console.log(this.hobbiesRooms);
      });
  }

I guess my only curiosity is why I am subscribing twice. Am I correct in thinking that I'm subscribing to the hobbyroomobj[] and then subscribing to the method gethobbiesbyId()?

1 Answer 1

1

You need to place the console.log inside the subscribe,

this.data.getHobbiesById().subscribe(
data => this.hobbiesRooms = data;
console.log(this.hobbiesRooms);
);
Sign up to request clarification or add additional context in comments.

8 Comments

Although, this doesn't solve my problem I appreciate your input. The only problem is the syntax doesn't seem to work. Could you help further explain why my previous console.log was not working correctly?
@user9639000 because you are having the console.log outside the subscribr where this.hobbiesRooms is not defined since the service returns data asynchronously
Gotcha that makes sense. I fixed the syntax and now I see what you are saying. I guess I'm still having a hard time understanding why the html of ul is not being updated now. But, this helps me understands subscribed observables. Can I say that? Subscribed observable?
The Hobby-View-Component.html is not updating for some reason. From what I have so far, The first HobbiesComponent has a list of hobbies, when one of those hobbies is selected (onSelect) it tells the Hobby-View-Component to call a service that subscribes a local array of hobbies to an observable that makes a get request to my local server. But the list never updates.
is your variable a subject?
|

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.