如何检查 firebase 数据库值是否存在? [英] How do I check if a firebase database value exists?

查看:24
本文介绍了如何检查 firebase 数据库值是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将实时数据库与 Google 的 Firebase 结合使用,我正在尝试检查是否存在孩子.

I'm using the Realtime Database with Google's Firebase, and I'm trying to check if a child exists.

我的数据库结构如下

- / (root)
-   /users/
–-    /james/
--    /jake/
-   /rooms/
--    /room1/
---      (room 1 properties)
--    /room2/
---      (room 2 properties)

我想检查room1是否存在.我尝试了以下方法:

I would like to check if room1 exists. I have tried the following:

let roomName:String = "room1"
roomsDB.child(roomName).observeSingleEventOfType(.Value) { 
(snap:FIRDataSnapshot) in
    let roomExists:Bool = snap.value != nil ? "TAKEN" : "NOT TAKEN"
 }

在访问 snap.value 时,它返回该房间属性的 JSON,但我如何检查房间 (/rooms/room1/) 是否存在开始?

In accessing snap.value it returns a JSON of the properties of that room, but how would I check if the room (/rooms/room1/) is there to begin with?

如果需要澄清,请评论

推荐答案

self.ref = FIRDatabase.database().reference()

   ref.child("rooms").observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.hasChild("room1"){

            print("true rooms exist")

        }else{

            print("false room doesn't exist")
        }


    })

这篇关于如何检查 firebase 数据库值是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆