【问题标题】:Firebase realtime database cannot query with integerFirebase 实时数据库无法使用整数查询
【发布时间】:2020-06-02 18:01:06
【问题描述】:

我有如下数据样本

{
  "random_key 1" : {
    "id": 0,
    "text": "This is text"
  },
  "random_key 2" : {
    "id": 1,
    "text": "This is text"
  }
}

我想查询 'id' 等于 1 的节点。

val database = FirebaseDatabase.getInstance().getReference()
database.orderByChild("id").equalTo(1)

但此代码返回错误,因为通过此链接关注谷歌文档:https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query

该库仅支持 Double、Boolean 和 String。我现在真的很困惑。怎么可能?

【问题讨论】:

  • 错误信息是什么?
  • 感谢您的回答。错误信息如下:None of the following functions can be called with the arguments supplied: @NonNull public open fun equalTo(p0: Boolean): Query defined in com.google.firebase.database.Query @NonNull public open fun equalTo(p0: Double): Query defined in com.google.firebase.database.Query @NonNull public open fun equalTo(@Nullable p0: String?): Query defined in com.google.firebase.database.Query

标签: android firebase kotlin firebase-realtime-database


【解决方案1】:

是的,你是对的。

该库仅支持 Double、Boolean 和 String

你的query 也是对的。作为火力基地,将1 视为double。所以,你的 query 应该可以工作。检查以下:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query query = databaseReference.orderByChild("id").equalTo(1);
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {

            String textValue = childSnapshot.child("text").getValue(String.class);
            
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

Kotlin 更新:在 Jave 中,intdouble 会使用赋值运算符自动转换,因为较低类型可以隐式转换为较高类型。这也称为隐式类型转换或类型提升

但在 Kotlin 中,没有隐式类型转换。您必须使用toDouble() 自己完成。检查以下:

val databaseReference = FirebaseDatabase.getInstance().reference
val query = databaseReference.orderByChild("id").equalTo(1.toDouble())
query.addListenerForSingleValueEvent(object: ValueEventListener {
    override fun onCancelled(dataSnapshot: DatabaseError) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onDataChange(dataSnapshot: DataSnapshot) {
        dataSnapshot.children.forEach { childSnapshot-> 
             
            val textValue = childSnapshot.child("text").getValue(String::class.java)
        }
    }
})

【讨论】:

  • 我收到了这个错误:None of the following functions can be called with the arguments supplied: @NonNull public open fun equalTo(p0: Boolean): Query defined in com.google.firebase.database.Query @NonNull public open fun equalTo(p0: Double): Query defined in com.google.firebase.database.Query @NonNull public open fun equalTo(@Nullable p0: String?): Query defined in com.google.firebase.database.Query 完全无法编译代码。感谢您的帮助!
  • @danh-nguyen,对不起 kotlin 语法。我已经更新了我的答案。请检查。
猜你喜欢
  • 2019-07-31
  • 1970-01-01
  • 2018-12-14
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多