Home > Back-end >  How to add variables from a firebase snapshot
How to add variables from a firebase snapshot

Time:12-16

I know this question has been ask several times but none of the solutions work for me. here is my snap shot

Snap (CaseExtend) {
    NYxdSlq8yOgQd3phssRlD =     {
        CaseMinute =         {
            WMKuImjuH0qPIGJhHHJKv =             {
                caseMinute = "";
                dateTime = "Dec 1, 2021 12:20 PM";
                minuteId = WMKuImjuH0qPIGJhHHJKv;
                userId = OtRFaqilTigWvEkUCASvCoEegny1;
            };
        };
        CaseParticipant =         {
            3f7nqmcWFTenePkEB9KoMHLzVtk2 =             {
                userId = 3f7nqmcWFTenePkEB9KoMHLzVtk2;
            };
            OtRFaqilTigWvEkUCASvCoEegny1 =             {
                userId = OtRFaqilTigWvEkUCASvCoEegny1;
            };
        };
        caseId = Oj7TwzDC3HQMJKyTb8Ebi;
        caseNumber = TEST;
        court = "Labour Tribunal";
        courtRoomNo = "";
        date = "2021-12-01";
        extendId = NYxdSlq8yOgQd3phssRlD;
        extendStatus = "Not Extend";
        location = "";
        participantCount = 2;
        profilePicture = "https://firebasestorage.googleapis.com/v0/b/lege-155e1.appspot.com/o/Profile Pictures/OtRFaqilTigWvEkUCASvCoEegny1.jpg?alt=media&token=676226fa-638b-4175-a8d5-247095f6b86a";
        step = TBM;
        time = "";
        userId = OtRFaqilTigWvEkUCASvCoEegny1;
    };
}

and i retrieve the snapshot using following code,

   ref.child("CaseExtend").queryOrdered(byChild: "userId").queryEqual(toValue: "OtRFaqilTigWvEkUCASvCoEegny1" ).observe(.value, with: { (DataSnapshot) in
            
            
        })

i want to add the caseID, caseNumber, date.. and other values for different variables. is there anyway i can do this?

CodePudding user response:

first of all you are getting all data in casetExtend node all the variables you want to assign is not under that node. for that you need to navigate one level of child node.

ref.child("CaseExtend").queryOrdered(byChild: "userId").queryEqual(toValue: "OtRFaqilTigWvEkUCASvCoEegny1" ).observe(.value, with: { (snapshot) in
            for caseSnapshot in snapshot.children.allObjects as! [DataSnapshot] {

let dict = caseSnapshot.value as? [String: AnyObject] // assign the snapshot to a dictionary
let court = dict!["court"] as? String  ?? "unknown"; // assign the variable

      }
        })
  • Related