如何在firebase (swift)中获取子节点的值,并将其显示在视图控制器的标签中

时间:2021-07-22 07:36:36

this is my firebase structure

这是我的壁炉结构

Users:

用户:

simple login 1:

简单登录1:

  • Name : mike
  • 名称:迈克
  • age : 24
  • 年龄:24
  • location : USA
  • 地点:美国

i am trying to get the info of currently logged in user and display it on a label in view controller.

我正在尝试获取当前登录用户的信息,并将其显示在视图控制器的标签上。

i have wrote the following code in the view did load :

我在did load的视图中写了如下代码:

override func viewDidLoad() {
    super.viewDidLoad()

    ref.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in

    println(snapshot.value["users\(self.ref.authData.uid)/age"] as? String)

    })

but when i run my app it prints "nil" in the console.what lines of code should i write to get info of child nodes? once i get it then i can easily set my labels according to it. please help me i am new to swift and firebase.

但是当我运行我的应用程序时它会在控制台打印“nil”。为了获得子节点的信息,我应该编写哪些代码行?一旦我得到它,我可以很容易地根据它设置我的标签。请帮我,我是雨燕和火垒新手。

MORE

更多的

Here's how i created Ref :

下面是我如何创建Ref:

var ref = Firebase(url: "https://chatty93.firebaseio.com/")

var userId = authData.uid

let newUser = [                       
  "name": self.Name.text,
  "location" : "",
  "age"      : "",
]

self.ref.childByAppendingPath("users")
    .childByAppendingPath(authData.uid)
    .setValue(newUser) 

the name is provided by the logged in user on this view controller and age and location are provided by the logged in user on another view controller.. here is what i coded on the other view controller :

此视图控制器上的登录用户提供名称,另一个视图控制器上的登录用户提供年龄和位置。下面是我在另一个视图控制器上编写的代码:

var age: Void  = ref.childByAppendingPath("users/\(ref.authData.uid)/age").setValue(AgeTextField.text)
var location: Void  = ref.childByAppendingPath("users/\(ref.authData.uid)/location").setValue(LocationTextField.text)
var About : Void = ref.childByAppendingPath("users/\(ref.authData.uid)/about").setValue(AboutTextField.text)

now on another view controller i just want this age location and name to be taken from firebase and displayed on labels but i just can't figure out how to do it .. i tried the observe event of single type but it is giving me nil.. please help me with this! thanks :)

现在,在另一个视图控制器上,我只想从firebase中获取这个年龄位置和名称,并在标签上显示出来,但是我不知道怎么做。我尝试了单一类型的观察事件,但它给了我nil。请帮帮我!谢谢:)

1 个解决方案

#1


1  

Try

试一试

ref.childByAppendingPath("users")
.childByAppendingPath(authData.uid)
.childByAppendingPath("age")
.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in

        println(snapshot.value)


    })

#1


1  

Try

试一试

ref.childByAppendingPath("users")
.childByAppendingPath(authData.uid)
.childByAppendingPath("age")
.observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in

        println(snapshot.value)


    })