如何在 Firebase 中检索自动生成的 id/key?

How to retrieve the auto generated id/key in Firebase?

我正在使用以下方法获取 firebase 数据库中的所有产品,但我需要获取自动生成的密钥。我应该怎么做才能做到这一点?

func fetchProducts(){
    self.ref.child("products").observe(.childAdded, with: { (snapshot) in
        //print(snapshot)
        let proObject = snapshot.value as! NSDictionary

        let description = proObject["description"] as! String
        let categories = proObject["category"] as! Array<String>
        let images = proObject["image"] as! Array<String>
        let name = proObject["name"] as! String
        let price = proObject["price"] as! Int
        let qty = proObject["qty"] as! Int

        let pro = Product(name: name, qty: qty, 
        images: images, price: price, category: categories, description: description)

        self.items.append(pro)
    })
}

自动生成的密钥在 snapshot.key 属性 中可用。

Firebase documentation 对于这个 属性:

The key of the location that generated this FIRDataSnapshot.