Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find record through HorizonIO in RethinkDB not returning result

I've got a MeetingService that fetches data from my RethinkDB through HorizonIO. When trying to fetch a meeting through its ID, I'll always get a null value as response. Other methods in this service work without issue.

meeting.service.ts:

getMeetingById(passedId: string): Observable<Meeting[]> {
    return this.table.find({meetingId: passedId}).fetch();
}

meeting-detail.component.ts:

currentMeeting: Meeting;

getCurrentMeeting(): void {
    this._meetingsService.getMeetingById(this.passedId).subscribe(
        (returnMeetings: Meeting[]) => {
            this.currentMeeting = returnMeetings[0];
        }
    );
}

Even when I change the passedId to an ID I got directly from my db (through the admin panel), the method still returns null.

If I change the code to return the first value in the table everything will simply work.

getMeetingById(passedId: string): Observable<Meeting[]> {
    return this.table.order("meetingId", "descending").limit(1).fetch();
}

The end result is error undefined on the currentMeeting var in my view:

EXCEPTION: Error in ./MeetingDetailComponent class MeetingDetailComponent - inline template:1:4 caused by: 
Cannot read property 'meetingId' of undefined

Edit: Additional code showing the different implementatations:

load() {
    if(this.passedId != null) {
      this._feedService.GetFeedById(this.passedId).subscribe(
          (returnFeed: Feed[]) => {
            this.currentFeed = returnFeed[0];
          }
      );
    } else {
      this._feedService.GetFirstFeed().subscribe(
          (returnFeed: Feed[]) => {
            this.currentFeed = returnFeed[0];
          }
      );
   }
}

Second one works, first one doesn't. The passed id is correct and and is also available in the database so this is quite confusing.

Thanks in advance.

like image 494
DGK Avatar asked Mar 09 '17 13:03

DGK


1 Answers

Although no one seems to care for the question, I've found an answer that works relatively well. My temporary fix involved fetching the entire table and manually finding the right id through a for each but it's also possible like this:

  1. Create a variable that's an observable of the table you need (through .watch())
  2. Call flatMap() and fetch the observable array (if anyone knows why this is necessary please let me know)
  3. Use find() and a lambda to call the right id.

    getMeetingById(passedId: string): Observable<IMeeting> {
        var allMeetings: Observable<Meeting[]>;
        allMeetings = this.table.watch();
    
        return allMeetings.flatMap((meetings: IMeeting[]) => Observable.from(meetings))
                          .find((meeting: IMeeting) => (meeting.id == passedId));
    
    }
    
like image 179
DGK Avatar answered Nov 09 '22 08:11

DGK