我有以下课程:
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);
}
}
我打电话给Firebase
并检索一个用户列表,这些用户的状态与我传入的位置匹配,如下所示:
getSearchResults(location: string): Observable<Results[]> {
return this.af.database.list('/user/', {
query: {
orderByChild: 'state',
equalTo: location
}
}).map(Results.fromJsonList);
}
然后我.map
对(Results.fromJsonList);
在static
方法fromJson
内,当我点击debugger
断点并检查传递的值时,它们都是正确的。
但是,当我在组件中记录结果时:
getSearchResultsComponent(gender: string, seeking: string, intent: string, location: string, hobbies?: string) {
this.searchSubscription = this.searchService.getSearchResults(location).subscribe(results => {
console.log(results);
});
}
我看到以下内容:
我应该看到两个用户的条目,即first_name,性别,位置等。
有人可以解释为什么这种映射不像我设想的那样工作吗?
答案 0 :(得分:1)
创建所有Results
类成员public
,以便可以通过对象实例访问它们。目前您只是将参数传递给Results
constructor
,但它们并未分配给任何类成员。
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
}