I have the following class:
import { Hobbies } from './hobbies';
export class Results {
constructor(
$key: string,
first_name: string,
gender: string,
intent: string,
seeking: string,
training: string,
latitude: string,
longitude: string,
location_desc: string,
state: string,
hobbies: Hobbies[],
cloud_Id: string) {
}
static fromJsonList(array): Results[] {
return array.map(json => Results.fromJson(json))
}
static fromJson({ $key, cloud_Id, first_name, gender, hobbies, intent, latitude, location_desc, longitude, seeking, state, training }): Results {
debugger;
return new Results(
$key,
first_name,
gender,
intent,
seeking,
training,
latitude,
longitude,
location_desc,
state,
hobbies,
cloud_Id);
}
}
I make a call to Firebase and retrieve a list of users who state matches the location I pass in as shown here:
getSearchResults(location: string): Observable<Results[]> {
return this.af.database.list('/user/', {
query: {
orderByChild: 'state',
equalTo: location
}
}).map(Results.fromJsonList);
}
I then .map the response to (Results.fromJsonList);
Inside the static method fromJson, when I hit the debugger breakpoint and check the values being passed, they're all correct.
However when I log the results within my component:
getSearchResultsComponent(gender: string, seeking: string, intent: string, location: string, hobbies?: string) {
this.searchSubscription = this.searchService.getSearchResults(location).subscribe(results => {
console.log(results);
});
}
I see the following:

Whereas I should be seeing the entries for two users, i.e first_name, gender, location etc.
Can someone explain to why this mapping isn't working the way I envisioned it working?
Make all Results class members public so that they can be reachable via object instance. Currently you just had parameters passed to Results constructor, they aren't got assigned to any class member.
export class Results {
constructor(
public $key: string,
public first_name: string,
public gender: string,
public intent: string,
public seeking: string,
public training: string,
public latitude: string,
public longitude: string,
public location_desc: string,
public state: string,
public hobbies: Hobbies[],
public cloud_Id: string
)
//other code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With