[英]How can I get a specific data in Firebase (Kotlin)
I need to retrieve the "administrator" value from Firebase我需要从 Firebase 中检索“管理员”值
This is the code I use:这是我使用的代码:
FirebaseDatabase.getInstance().getReference("/restaurants/$restaurantUID/administrator")
.get().addOnSuccessListener {
Log.d("key", it.toString())
}
The result in the log is this though: 2022-09-05 17:07:32.271 2885-2885/com.example.menuapp D/key: DataSnapshot { key = administrator, value = null }日志中的结果是这样的: 2022-09-05 17:07:32.271 2885-2885/com.example.menuapp D/key: DataSnapshot { key = administrator, value = null }
How can I get the actual value for "administrator" and not a null?如何获得“管理员”的实际值而不是 null?
According to your last comment, I see that value of restaurantUID
is:根据您的最后评论,我看到restaurantUID
的值是:
eefef966-f227-45e8-b875-f568b87ee1fc
Which is totally different than the one in the database.这与数据库中的完全不同。 The one in the database strats with "c20f02b0-...", hence that null for the value of administrator
filed.数据库中的一个带有“c20f02b0-...”,因此 null 为administrator
归档的值。 If you want to get the correct value, then always make sure you're reading the data on the correct path:如果您想获得正确的值,请始终确保您在正确的路径上读取数据:
val restaurantUID = "eefef966-f227-45e8-b875-f568b87ee1fc"
val db = FirebaseDatabase.getInstance().reference
val adminRef = db.child("restaurants/$restaurantUID/administrator")
adminRef.get().addOnSuccessListener {
Log.d("key", it.toString())
}
Try this -尝试这个 -
FirebaseDatabase.getInstance().getReference("/restaurants/$restaurantUID").orderByChild("administrator").equalTo(“Alr6tf3nzTNTXbx53ysbcAxcM643”).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
System.out.println(dataSnapshot.getKey());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.