Skip to content Skip to sidebar Skip to footer

Firebase Database: Why Do The Key/ref In My Queried Datasnapshot Refer To Its Parent?

I would like to delete an entry from the realtime database that i looked up through a query but i am getting unexpected results for both the ref and key properties. ref('photos/use

Solution 1:

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

Your code needs to take this list into account, and iterate over snapshot.forEach() to get the actual matching item:

ref('photos/users/USERID')
    .orderByChild('photoname')
    .equalTo('photoname i want to look up')
    .limitToFirst(1)
    .query('once')
    .then(snapshot => {
      snapshot.forEach(child => {
        const entry = child.val();     
        constref = child.ref;     
        const key = child.key
      });
    })

Solution 2:

Your ref and key are based on ref('photos/users/USERID'), not the filtered subset you have created. Is there any reason you wouldn't call ref('photos/users/USERID/PHOTOID')?

Post a Comment for "Firebase Database: Why Do The Key/ref In My Queried Datasnapshot Refer To Its Parent?"