2016-01-13 69 views
0

我在前端使用Firebase作爲我的數據庫和React。我試圖顯示用戶特定的數據(註釋)並顯示它。我已經詢問了similarquestions SO,但我認爲它們太開放了,所以這是更具體的:使用ReactFire或Re-base從Firebase顯示用戶特定數據

我已經按照Firebase's denormalized suggestions組織我的數據。下面就一起來看看如何將數據結構:

"notes" : { 
    "n1" : { 
     "note" : "[noteData]", 
     "created_at" : "[date]", 
     "updated_at" : "[date]", 
    } 
}, 
"users" : { 
    "userOne" : { 
     "name" : "[userName]", 
     "notes" : { 
      "n1" : true 
     } 
    } 
} 

由於其他評論者,我已經能夠爲使用ReactFireTyler McGinnis's Re-base搶音符鍵的每個用戶的列表,並在state創建註釋對象。這看起來像這樣:

ReactFire

firebaseRef.child('users/' + authData.uid + '/notes').orderByChild('date_updated').on("child_added", function(noteKeySnapshot) { 
    ref = firebaseRef.child('notes/' + noteKeySnapshot.key()); 
    this.bindAsObject(ref, noteKeySnapshot.key()); 
}.bind(this)); 

重新基地

firebaseRef.child('users/' + authData.uid + '/notes').orderByChild('date_updated').on("child_added", function(noteKeySnapshot) { 
    base.syncState('notes/' + noteKeySnapshot.key(), { 
     context: this, 
     state: noteKeySnapshot.key(), 
     asArray: false, 
    }); 
}.bind(this)); 

這兩種解決方案的每一個音符對象添加到this.state樹的頂端。這使得難以作爲一組訪問筆記。我想將每個音符對象添加到數組中,例如this.state.notes

如何使用ReactFire或Re-base將每個音符對象添加到this.state的數組中?

我可以在this.state中找到有關如何訪問這些對象的其他解決方案。

回答

0

如果我沒有弄錯,你必須在放置快照的地方設置它。

在Github的實施例

componentDidMount(){ 
    base.syncState(`notes`, { 
    context: this, 
    state: 'notes', //Changes this to whatever you want as inner state. 
    asArray: true 
    }); 
} 
相關問題