2017-09-21 25 views
2

我使用Angular4(4.3.6)和AngularFire2(4.0.0-rc.2)。我得到的数据与细节图:AngularFire2 - 使用db.object获取单个对象 - 数据来迟

// GET THE ID 
this.id = this.route.snapshot.params['id']; 

this.subscriptions.push(
    this.db.object('/restaurants/' + this.id).subscribe(data => { 
    this.restaurant = data; 
    console.log(data); 
    }) 
); 

这样,我得到的数据,但控制台抛出错误,因为该数据来自一个有点太晚了。最后我不喜欢从route.params中获取id,因为我想以后使用对象名称作为详细视图的路由。

这里一个StackBlitz:https://stackblitz.com/edit/angular-7gabaq?file=sites/site-restaurant-detail/site-restaurant-detail.component.ts(点击 “查看餐厅”)

回答

0

创建项目时,您可以尝试制作自定义键: 路径= restaurants/${res_name}

newForm(res_name: string, data_1: string, data_2: string) { 
const path = `restaurants/${res_name}`; // Endpoint on firebase 
const userRef: AngularFireObject<any> = this.db.object(path); 
const data = { 
    restaurant: res_name, 
    some_data: data_1, 
    onether_data: data_2 
}; 
userRef.update(data) 
    .catch(error => console.log(error); 

}