我正在尝试创建一个 View Controller ,显示来自前一个 View Controller 上的 UITableView 的已点击商店的信息。但是,我无法在新的 View Controller 中检索数据,而且我不知道如何解决这个问题。这是我的数据库结构。感谢您的帮助。
import UIKit
import Firebase
import FirebaseDatabase
class ShopViewController: UIViewController {
var name :String? // This is the name of the cell clicked on the previous viewcontroller
var ref: DatabaseReference!
@IBOutlet weak var shopName: UILabel!
@IBOutlet weak var shopType: UILabel!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
shopName.text = name
let ref = Database.database().reference().child("shops").childByAutoId().child(name!).child("Details")
ref.observe(.childAdded, with: { snapshot in
let snapshotValue = snapshot.value as! NSDictionary
let imageUrlString = snapshotValue["imageURL"] as! String
print(imageUrlString)
let shoptype = snapshotValue["type"] as! String
self.shopType.text = shoptype
})
}
}
最佳答案
您的引用有误,childByAutoId()
用于在您的数据库中生成唯一 ID。
另外你现在的id是userid,你需要取回userid:
let user = Auth.auth().currentUser
let uid = user.uid
那么位置应该是:
let ref = Database.database().reference().child("shops").child(uid).child("Eat").child("Details")
关于ios - 无法使用 Swift 从 Firebase 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49750994/