FIRInvalidArgumentException, reason: Document path cannot be empty

317 Views Asked by At

I am trying to access some values in a dictionary I made from Firebase snapshot, I use these values to access some document in my firestore, but these values come back empty therefore the path I used those values in, is empty, and I can't access the firestore.

I am not sure if the values are nested in the dictionary, I tried searching for accessing nested dictionary, but I got other errors.

Here is the output

snapshot Optional({
16640500463403668 =     {
    ReciverID = "[email protected]";
    RewardKey = "Reward_25";
    isScanned = 1;
};
}) 
Scanned successfully 
16640500463403668 
Dictionary is:
Optional({ 16640500463403668 = { ReciverID = "[email protected]"; RewardKey = "Reward_25"; isScanned = 1; }; }) 
resciver id is
RewardKey id is

2022-09-25 02:31:20.924590+0300 KhutaaScannser[30891:1892245] *** Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Document path cannot be empty.'

And here is my code:

let ref = ref.child("AllQRCodes")
ref.getData { [self] (error, snapshot) in
    print("snapshot \(snapshot.value as Any)")
   
    if let value = snapshot.value as? [String: Any] {
        for key in value.keys {

            if key == QRnumber {
        
                self.ref.child("AllQRCodes").child(QRnumber).child("isScanned").setValue(true)
                print("Scanned successfully")
                self.messageLabel.text = "Succefully Scanned"
                print(key)
                
                let dictionary = snapshot.value as? NSDictionary
                print("Dictionary is:  " )
                print(dictionary)
                
                var ReciverID = dictionary?["ReciverID"] as? String ?? ""
                var RewardKey = dictionary?["RewardKey"] as? String ?? ""
                
                print("resciver id is" + ReciverID)
                print("RewardKey is "+RewardKey)
                
                db.collection("users").document(ReciverID).collection("UserRewards").document(RewardKey).setData( ["isScanned": true], merge: true)
                
                print("Scanned successfully")
                self.messageLabel.text = "Succefully Scanned"

                self.ref.child("AllQRCodes").child(QRnumber).removeValue()
                
                break
1

There are 1 best solutions below

0
Frank van Puffelen On

You're not reading from the correct child snapshot.

var ReciverID = dictionary?["QRnumber"]["ReciverID"] as? String ?? ""

You might need some extra null handling in there, so if you get an error about that, please search for the error message.