本文介绍了Firebase数据库:为什么我查询的数据快照中的键/引用引用其父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从实时数据库中删除我通过查询查找的条目,但是我在ref和key属性上都得到了意外的结果.

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/users/USERID')
    .orderByChild('photoname')
    .equalTo('photoname i want to look up')
    .limitToFirst(1)
    .query('once')
    .then(snapshot => {
        // correct data at 'photos/users/USERID/PHOTOID'
        const entry = snapshot.val();

        // seems to be the ref to photos/users/USERID'
        const ref = snapshot.ref;

        // seems to be USERID
        const key = snapshot.key
    })

为什么这些不是我刚刚找到的条目的ref/键?删除此条目的最佳方法是什么?

Why aren't these the ref/key to the entry i just found? And what is the best approach to remove this entry?

推荐答案

对Firebase数据库执行查询时,可能会有多个结果.因此,快照包含这些结果的列表.即使只有一个结果,快照也会包含一个结果的列表.

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.

您的代码需要考虑此列表,并遍历snapshot.forEach()以获得实际的匹配项:

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();
        const ref = child.ref;
        const key = child.key
      });
    })

这篇关于Firebase数据库:为什么我查询的数据快照中的键/引用引用其父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:50