5

I came back to work on a project I have not touched in a week and suddenly my code was not working. I take a snapshot of data that I am sure exists (I can see it exactly in the database and changed no code) yet it is returning NSNULL for the snapshot and when I print snapshot.exists() I get false. I even tried printing snapshot.ref and typing that link into my browser and that takes me to the data on the console.

The only thing I did today was manually add a piece of data into my firebase console, but it was not in the same node where I am getting this error and it was working for a while after I made the change so I don't see how that could have caused this. Is anyone else having this issue or know why this would suddenly start happening?

Here is my code:

_ =     ref.child("profiles").child((FIRAuth.auth()?.currentUser?.uid)!).child("type").observeSingleEventOfType(FIRDataEventType.Value, withBlock: { (snapshot) in
        print(snapshot.exists())
        print(snapshot.ref)
        if (snapshot.value as! String == "Player") {
             .....
        }
})

I'm getting the error Could not cast value of type 'NSNull' (0x10d008600) to 'NSString' (0x10d3ffb48).' and as I said earlier snapshot.exists() is false and the ref link works.

Thanks!

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alex
  • 299
  • 3
  • 16
  • Give your JSON tree.Did you change the security rules? – Dravidian Sep 07 '16 at 22:23
  • added the data pic, I did not change security rules. Could there be a setting that causes this? – Alex Sep 07 '16 at 22:29
  • try printing the current user ID and match it with your DB.. Are they same? – Dravidian Sep 07 '16 at 22:32
  • Yes. I double checked and also I know the reference is correct because using the link from snapshot.ref works in my browser and takes me to the correct place. – Alex Sep 07 '16 at 22:36
  • @Alex, did you ever solve this? I'm experiencing the same error in one of my queries and its driving me insane – MattEm Feb 18 '17 at 00:24
  • I didn't really solve it, but I got aruond it by adding a check of if snapshot.exists() before getting the value and when I did that somehow my code executed normally. I think it had something to do with the timing of the queries being executed (I had a query embedded in another query) so somehow adding this check forced it to behave correctly – Alex Feb 18 '17 at 04:48
  • I'm have the same error. Have you solved this? – MMSousa Mar 08 '17 at 18:39
  • Same. I have guards snapshot.exists() but they still don't work. – Script Kitty Mar 16 '17 at 02:55
  • Look this post friend, is about js, but may work for you (asynchronous in nature): https://stackoverflow.com/questions/28381006/firebase-query-inside-function-returns-null – Harold Sarmiento Jul 02 '17 at 06:31

0 Answers0