[英]angularfire2 snapshot data
Iam new to angularfire 2 and having a problem. 我是angularfire 2的新手,但遇到了问题。 I have a set of restaurantS stored in the cloudflare of firebase. 我在firebase的cloudflare中存储了一组restaurantS。 I am able to get the list of restaurants correctly and display it, but when i try to access and save a particular restaurant to a separate variable, it returns an observable which I cant seem to get the data from: 我能够正确获取餐厅列表并显示它,但是当我尝试访问并将特定餐厅保存到单独的变量时,它返回一个observable,但我似乎无法从中获取数据:
items: Observable<any[]>;
tester: any[] = [];
private test: AngularFirestoreCollection<any>;
constructor(db: AngularFirestore){
this.test = db.collection<any>('restaurants')
// this.items = db.collection('restaurants').valueChanges();
this.items = this.test.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
this.tester.push(data); //This returns an observable instead of an array
const id = a.payload.doc.id;
return { id, ...data };
});
});
}
getRestaurants(){
return this.items;
}
//Here I want to be able to return the item object
getRestaurant(index){
console.log(this.tester[i]);
}
Component code 组件代码
constructor(private restService: RestaurantService){}
restaurants = <any>[];
selectedRestaurant: any;
ngOnInit(){
this.restaurants = this.restService.getRestaurants()
}
onSelect(index){
console.log(this.restService.getRestaurant(index));
}
Im struggling to understand how I can extract data from the returned Observable list from firebase. 我正在努力了解如何从Firebase返回的Observable列表中提取数据。
If you want to get at the "current value" of an Observable, you should use a BehaviorSubject. 如果要获得Observable的“当前值”,则应使用BehaviorSubject。
restaurants: BehaviorSubject<any[]>;
private test: AngularFirestoreCollection<any>;
constructor(db: AngularFirestore){
this.test = db.collection<any>('restaurants')
this.test.snapshotChanges().subscribe(this.restaurants);
}
getRestaurants(){
return this.restaurants.value();
}
getRestaurant(index){
return getRestaurants()[index]; //.. or whatever you need
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.