'Firebase not nesting data under userID - Kotlin // Android Studio // Firebase Realtime Database
I'm busy creating an app that has swipe features (like Tinder) and I'm stuck on the "Swipe Left" functionality. Specifically adding the data (who swiped left on who) in the appropriate place in the database.
I would like it to look as follows:
- Users -UserID (swiped) --Name: --Email: (etc etc) --swiped left: ["swiper" uid] - true
edit: The above is not looking like it should... :-/
However, no matter what I try, it ends up nesting the "Swiped left" under Users, not under the specific UserID. Here is what it looks like
On a bit of trial and error, I've managed to narrow it down to the fact that it's not picking up the uid child (I hope this makes sense).
Here is the code in question:
override fun onLeftCardExit(p0: Any?) {
var user = p0 as User
userDatabase.child(user.uid.toString()).child(DATA_SWIPES_LEFT).child(userId).setValue(true)
}
If I change "userDatabase.child(user.uid.toString())" to show "...name.toString()" instead of uid, it also nests it under Users, but it actually shows the "swiped" user's name. just "...uid.toString()" shows ONLY the "swiper's" uid and "true" - here's what I mean:
override fun onLeftCardExit(p0: Any) {
val swipedLeftUser = p0 as User
val swipedLeftUserId = swipedLeftUser.name.toString()
userDatabase.child(swipedLeftUserId).child(DATA_SWIPES_LEFT).child(userId).setValue(true)
}
My question is - how do I nest "swiped left" under the "Swiped" uid, with the "swiper" info? What am I missing?
Let me know if there's any further info needed. The frustration is real.
Solution 1:[1]
I think you need to get the key of the entry because you need to nest it. I am not an expert of Kotlin but I think you should do something like this:
val key: String = it.key.toString()
userDatabase.child(swipedLeftUserId).child(DATA_SWIPES_LEFT).child(userId).child(key).setValue(true)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Francesco D'Onorio De Meo |
