I want to check if the bus number already exists in the database of Firebase.
Here\'s my sample code. I\'ve been searching for the past days but I can\'t find the rig
It is difficult to guess the problem because you do not show how busNum
and busNumber
are defined and managed. Is busNumber
a String?
push()
creates a reference to an auto-generated child location. The auto-generated key looks something like -KPB_cS74yoDaKkM9CNB
.
The statement postRef.push().setValue(busNumber)
stores value busNumber
in location BusNumber/
.
The statement dataSnapshot.child(busNum).exists()
tests for the existence of a value at location BusNumber/
. It will not be true unless busNum
is one of the keys created by push()
.
It's not clear how you want your data structured. If your bus numbers are Strings and are unique, you do not need to generate a key with push()
. You could store the existence of bus numbers using:
postRef.child(busNumber).setValue(true)