在firebase中我会错过Parsing JSON吗?

时间:2017-01-21 20:30:08

标签: ios json swift firebase firebase-realtime-database

我花了很多时间,我无法弄明白。为什么它不解析。 enter image description here

func fetchUser(_ uid: String) {
    FIRDatabase.database().reference().child("users").child("g373gSvDcFhXNAGh5V6vg7sU5C02").observe(.childAdded, with: {
        (snapshot) in
        //TODO...

        print(snapshot) //Snap (email) test1@gmail.com
        let user = snapshot.value as ? [String: Any]

        print(user) //nil

        if let _nameLabel = self.nameLabel,
            let name = user ? ["name"] as ? String {
                _nameLabel.text = "{-} \(name)"
            }

    }, withCancel: nil)

}

1 个答案:

答案 0 :(得分:2)

<强>问题:

您使用事件类型.childAdded进行观察

  

将一个新的子节点添加到某个位置时触发了childAdded

<强>解决方案:

您应该将事件类型从.childAdded更改为.value,如下所示:

FIRDatabase.database().reference().child("users").child("g373gSvDcFhXNAGh5V6vg7sU5C02").observe(.value, with: { (snapshot) in

  // other code

})