2017-12-16 14 views
0

接続のコレクションからデータを取得し、私は、これらのコレクションがありますので、それは私が最後の2枚を得るのですかsheets/userId/sheetId/sheetDataは、私が反応してFirebaseを使用してい

ように書き

enter image description here

/sheetsDataのすべてのデータを素敵な形式で保存しています(この例では2枚のみです)ので、たとえばReduxストアに保存することができます。ありがとう

答えて

1

まず、userIDを取得する必要があります....これを行う方法はたくさんあります。私が個人的に使用する2つの例を以下に示します。

// ********* Add a listener from the database to monitor whos logged in. ********* 
firebase.auth().onAuthStateChanged((user) => { 
    // ********* If a user is logged in firebase will return the user object. THEY ARE NOT LOGGED IN THOUGH ********* 
    if (user) { 
    console.log('onAuthStateChanged', user) 
    // ********* Then we call an official Firebase function through actions, this would probably be getSheets() for you ********* 
    this.props.loginRequest(user); 
    } else { 
    console.log('No user signed in') 
    } 
}); 

// ********* After logging in the found user from above we need to set them to redux store ********* 
let signedInUser = firebase.auth().currentUser; 

if (signedInUser) { 
    this.props.loginRequest(signedInUser); 
    console.log('currentUserSignedIn', signedInUser) 
} else { 
    console.log('no active user', signedInUser) 
} 

ユーザIDを取得した後、あなたは、オブジェクトのオブジェクト内にすべてのシートの持つこのユーザーのスナップショットを取得するためにReduxのアクションを呼び出すことができます。

firebase.database().ref('users/' + user.uid).once('value').then(function (snapshot) { 
     // ******** This method is straight from their docs ******** 
      // ******** It returns whatever is found at the path 
      xxxxx/users/user.uid ******** 
     let username = snapshot.val(); 
     console.log(' FOUND THIS USER FROM THE DB', username); 
     // now dispatch whatever redux store action you have to store the user 
      information 
     dispatch(userSet(username)) 
     }) 
.catch((err) => console.log(err)); 

最後に、私はあなたが新しいシートごとにユニークなIDを作成しますfirebaseのプッシュ方式を使用しているように、現在それが見えるのでオブジェクトの配列としてのシートを収納お勧めします。 firebaseはオブジェクトのオブジェクトを返すので、CANTは.map、.filter、または.forEachをそれぞれ固有のIDを知らなくても使用できるので、これを操作することは困難です。ここで

は私がローカルオブジェクトをパッケージ化し、firebaseの深いネストを避けるために、一度設定したいログインhttps://github.com/GavinThomas1192/motoMechanicMeeKanic/blob/master/App/Actions/auth-actions.js

とfirebaseを使用しています方法です。

+1

返信をありがとう、私は間違いなくオブジェクトの配列として格納されます、なぜ私はそれをしていないか分からない。 – Case09

関連する問題