简体   繁体   English

Firebase权限被拒绝

[英]Firebase permission-denied

I'm a newbie in firebase. 我是firebase的新手。

How do I get through this below rule? 我如何通过以下规则?

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

I've tried to change the rule to below, 我试过将规则改为以下,

{
  "rules": 
   {
    ".read": true,
    ".write": true,
   }
}

but there's an error of 但是有一个错误

mismatched input '{' expecting {'function', 'service', 'syntax'}

Below is the db structure. 下面是db结构。

数据库结构

Currently, this is my code (which keeps returning Permission-denied): 目前,这是我的代码(不断返回Permission-denied):

    // [START initialize_database_ref]
    mDatabase = FirebaseDatabase.getInstance().reference
    // [END initialize_database_ref]

    val result = HashMap<String, Any> ()
    result.put("timestamp", getCurrentTime())
    result.put("user_id", USER_ID)
    result.put("x_position", -1)
    result.put("y_position", -1)

    mDatabase!!.ref.child("raw data").child("Z9QD79lDzP7MpD6feWeJ").setValue(result).addOnFailureListener(object : OnFailureListener {
        override fun onFailure(@NonNull e: Exception) {
            Log.d("firebase", e.localizedMessage)
        }
    })

Any help would be appreciated! 任何帮助,将不胜感激! Thanks :) 谢谢 :)

I was able to solve the problem by switching from Cloud Firestore to Realtime Database, and then changing the rule. 我能够通过从Cloud Firestore切换到实时数据库,然后更改规则来解决问题。 (After I've changed this, there was no more error showing!) (在我改变之后,没有更多的错误显示!) 在此输入图像描述

For others struggling with this problem, the proper solution is to change the false in 对于其他正在努力解决这个问题的人来说,正确的解决办法就是改变false

service cloud.firestore { 
    match /databases/{database}/documents {
        match /{document=**} { 
            allow read, write: if false; 
        } 
    }
}

To true while testing. 测试时为true Don't forget to add additional security when going live with your project! 在项目上线时不要忘记添加额外的安全性!

You should check in your Angular NgModule and components if you are using "Realtime Database" or "Cloud Firestore". 如果您使用的是“实时数据库”或“Cloud Firestore”,则应该检查Angular NgModule和组件。 See diferences using "angularfire2": 使用“angularfire2”查看差异:

REALTIME DATABASE 实时数据库

@NgModule({
...
imports: [
    AngularFireDatabaseModule,
    ...
],
providers: [
AngularFireDatabase,
...
]
})


@Component({...})
export class FirestoreComponent{
   constructor(private realtimeDb:AngularFireDatabase, ...) {
       this.locations = realtimeDb.list('locations').valueChanges();
     }
...
}

CLOUD FIRESTORE 云彩FIRESTORE

@NgModule({
    ...
    imports: [
       AngularFirestoreModule,
       ...
    ]
})

@Component({...})
export class FirestoreComponent{
   constructor(private firestore:AngularFirestore, ...) {
       this.locations = firestore.collection('locations').valueChanges();
     }
...
}

Because your method query the Firebase not Cloud Firestore so you should use this method : 因为您的方法查询Firebase而不是Cloud Firestore,所以您应该使用此方法:

FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("YourCollectionName").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Log.d(TAG, document.getId() + " => " + document.getData());
                }
            } else {
                Log.w(TAG, "Error getting documents.", task.getException());
            }
        }
    });

dont forget this : 别忘了这个:

implementation 'com.google.firebase:firebase-firestore:17.0.1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM