【发布时间】:2019-03-26 18:51:28
【问题描述】:
我正在尝试使用 firebase 后端编写测试 android-app。 我根据教程进行了所有设置(使用谷歌登录进行身份验证和实时数据库中根对象的身份验证检查规则),但仍然不能让firebase返回一些东西,但快照的值是空的(当 onDataChange() 被调用时)。 我确定路径和其他设置是有效的 - 否则我会在 onCancelled() 回调中得到“Permission Denied”。 我能做错什么?
代码:
mDatabase = FirebaseDatabase.getInstance().getReference("api");
mDatabase.child("test")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot
dataSnapshot) {
if (dataSnapshot.exists()) {
//code that's never called
}
}
@Override
public void onCancelled(@NonNull DatabaseError
databaseError) {
//log error
}
});
依赖关系:
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-auth:16.0.4'
数据结构:
{"test" : true}
快照的 toString() 结果:
DataSnapshot { key = test, value = null }
我可以从 Firebase 成功检索到的唯一一件事(如果它确实重要的话)是:
FirebaseDatabase.getInstance().getReference(".info/serverTimeOffset");
数据库规则:
{"rules": {
"api":{
".read": "auth != null",
".write": "auth != null",
}}}
感谢您的帮助。
【问题讨论】:
-
您的数据库规则是否设置为读取:true,写入:true?
-
发生这种情况的原因有两个,要么您的规则不符合您的意图,要么您的数据库结构与您尝试实现的不同,在此代码中.调查这些方面。
-
我已经用当前规则更新了我的问题 - 我在 web 控制台模拟器中使用这个规则检查了我的路径 - 一切看起来都很好,允许读取,但我注意到模拟器中的数据属性 respose也为空(但可能对模拟器没问题):类型读取位置 /api/test Data null Auth { "provider": "google", "uid": "53534d75-a8c2-440c-8c11-6cd043f415d8" } Admin false
标签: firebase firebase-realtime-database