简体   繁体   English

使用orderByChild和equalTo在Firebase查询上返回单个孩子的值

[英]Returning a single child's value on Firebase query using orderByChild and equalTo

I am trying to pull a URL for an image in storage that is currently logged in the firebase real time database. 我正在尝试为Firebase实时数据库中当前记录的存储中的图像提取URL。

This is for a game of snap - there will be two cards on the screen (left image and right image) and when the two matches the user will click snap. 这是用于捕捉游戏的-屏幕上将有两张卡片(左图和右图),当两张牌匹配时,用户将单击捕捉。

All of my image urls are stored in the following way: 我所有的图像网址都以以下方式存储:

数据库架构

Each one has a unique child called "index" - I also have another tree that is just a running count of each image record. 每个人都有一个唯一的称为“索引”的孩子-我还有另一棵树,它只是每个图像记录的运行计数。 So currently I am running a function that checks the total of the current count, then performs a random function to generate a random number, then performs a database query on the images tree using orderByChild and an equalTo that contains the random index number. 因此,当前我正在运行一个函数,该函数检查当前计数的总数,然后执行随机函数以生成随机数,然后使用orderByChild和包含随机索引号的equalTo对图像树执行数据库查询。

If I log the datasnap of this I can see a full node for one record (So index, score, url, user and their values) however if I try to just pull the URL I get returned a value of Null. 如果我记录它的数据快照,那么我可以看到一条记录的完整节点(因此,索引,分数,URL,用户及其值),但是如果我尝试仅提取URL,则会返回Null值。 I can, rather annoyingly, return the term "URL" seemingly at my leisure but I can't get the underlying value. 看起来很烦,我可以很随意地返回“ URL”一词,但我无法获得潜在的价值。 I've wondered if this is due to it being a string and not a numeric but I can't find anything to suggest that is a problem. 我想知道这是否是因为它是一个字符串而不是一个数字,但是我找不到任何暗示这一问题的信息。

Please bare in mind I've only been learning Javascript for about a week at max, so if I'm making obvious rookie errors that's probably why! 请记住,我最多只学习Java语言大约一个星期,因此,如果我犯了明显的菜鸟错误,这可能就是原因!

Below is a code snippet to show you what I mean: 下面是一个代码片段,向您展示我的意思:

    var indRef = firebase.database().ref('index')
    var imgRef = firebase.database().ref('images')
    var leftImg = document.getElementById('leftImg')
    var rightImg = document.getElementById('rightImg')

    document.addEventListener('DOMContentLoaded', function(){

    indRef.once('value')
    .then(function(snapShot){

        var indMax = snapShot.val()
        return indMax;
    })
    .then(function(indMax){


        var leftInd = Math.floor(Math.random()* indMax + 1)
        imgRef.orderByChild('index').equalTo(leftInd).once('value', function(imageSnap){
            var image = imageSnap.child('url').val();
            leftImg.src=image; 
            })
        })
    })

When you execute a query against the Firebase Database, there will potentially be multiple results. 当您对Firebase数据库执行查询时,可能会有多个结果。 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 cater for that list, by looping over Snapshot.forEach() : 您的代码需要通过遍历Snapshot.forEach()来满足该列表的需要:

   imgRef.orderByChild('index').equalTo(leftInd).once('value', function(imageSnap){
       imageSnap.forEach(function(child) {
         var image = child.child('url').val();
         leftImg.src=image; 
       })
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM