2017-09-21 58 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); 

}