Tarvo Mäesepp
Tarvo Mäesepp

Reputation: 4533

Retrieving specific data from firebase

I am trying to retrieve some data from my Firebase database but I can't get it done. First I created .json file with data and then imported it into Firebase. It looks like that but it is much longer:

enter image description here

Now I am trying to print out all the "Brands" like "CATCH", "CRAFTED SNUS" etc but I can't for some reason.

Thats how I am doing it. I am loading it inside viewDidLoad(). I know it is not right to do but just for testing:

func fetchBrands(){
    ref = FIRDatabase.database().reference()
    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

        if !snapshot.exists() { return }

        //print(snapshot)

        if let snusBrand = snapshot.value!["Brands"] as? String {
            print(snusBrand)
        }

        // snapshot.childSnapshotForPath("full_name").value as! String
    })


}

What I am doing wrong? I also set the read and write rules from Firebase to true

The only thing it prints is this:

enter image description here

Upvotes: 0

Views: 374

Answers (1)

Jean Bernard
Jean Bernard

Reputation: 73

You can try updating the ref to point directly to the child Brands and iterating on the returned snapshot like this:

ref = FIRDatabase.database().reference().child("Brands")

ref.observeEventType(.Value, withBlock: { snapshot -> Void in
    for brands in snapshot.children {
      print(brands)
    }
  })

Upvotes: 2

Related Questions