0

I'm trying to use firebase.database().ref('/friends/requests/').equalTo(currentUser.uid) in . the below code to retrieve the data only for the current user that is signed in. I'm very confused on how to filter it by user name and tried multiple scenarios to no avail.

I already tried using the equalTo method from firebase but cannot seem to get it to work. How can I access the second child of my data structure and still retrieve the data beneath it? the error is in the friendRequestFetch function.

JSON

{ "requests" : { "RIXXnKMGxYRPBTe1kBUsNUV9eFL2" : { "NQgSxcVKm1N3tnkykUevBM45JLn2" : { "name" : "Tom Hanks", "photoURL" : "https://firebasestorage.googleapis.com/v0/b/myapp-d0755.appspot.com/o/profilePhotos%2FNQgSxcVKm1N3tnkykUevBM45JLn2?alt=media&token=7822d763-e1e0-421e-88bf-836b34184c2c", "receiver" : "RIXXnKMGxYRPBTe1kBUsNUV9eFL2", "sent" : 1557800380001, "status" : "pending", "uid" : "NQgSxcVKm1N3tnkykUevBM45JLn2" } } } } 

Code

export const sendFriendRequest = (item) => { const { currentUser } = firebase.auth() return (dispatch) => { firebase.database().ref(`/friends/requests/${item.uid}/${currentUser.uid}`)//sting interpolation .set({ uid: currentUser.uid, //userName: firebase.database().ref(`/users/${currentUser.uid}`).snapshot.val().userName, name: currentUser.displayName, photoURL: currentUser.photoURL, status: "pending", sent: firebase.database.ServerValue.TIMESTAMP }) .then(() => { dispatch({ type: SEND_FRIEND_REQUEST }); }); }; }; export const friendRequestFetch = () => { const { currentUser } = firebase.auth() return (dispatch) => { firebase.database().ref('/friends/requests') .on('value', snapshot => { snapshot.forEach(child => { //anytime we get any value call function with an object thats describing the data thats sitting in there dispatch({ type: FRIEND_REQUEST_FETCH_SUCCESS, payload: child.val() // this is how we get access to the data at this ref }); }); }); }; }; 

I need the child vals but only for the current user that is signed in.

2
  • Can you replace the schematic with the actual JSON (as text, no screenshots)? You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your Firebase Database console. Commented May 14, 2019 at 3:16
  • I added the actual JSON. Thanks! Commented May 14, 2019 at 16:07

1 Answer 1

1

You can get the current logged in user by following two methods.

1)

firebase.auth().onAuthStateChanged(function(user) { if (user) { // User is signed in. } else { // No user is signed in. } }); 

2)

var user = firebase.auth().currentUser; 

in your case currentUser is equal to firebase.auth(). not firebase.auth().currentUser;. I think the issue is there.

for more info, refer the firebase documentation https://firebase.google.com/docs/auth/web/manage-users

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.