Firebase权限被拒绝

11

我是firebase的新手。

我该如何遵守下面这个规则?

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

我试图将规则更改为以下内容:

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

但是出现了一个错误

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

以下是数据库结构。

db structure

目前,这是我的代码(一直返回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)
        }
    })

非常感谢您的帮助!谢谢 :)

4个回答

38

我通过从Cloud Firestore 切换到Realtime Database,并更改规则来解决了问题。(在更改后,就不再显示错误!)enter image description here


3

对于其他在解决此问题时遇到困难的人,正确的解决方案是更改false

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

在测试时,将true设置为真。 不要忘记在上线项目时添加额外的安全措施!


我也尝试过那个方法,但仍然遇到了权限被拒绝的问题。虽然我已经用另一种方式解决了这个问题,但我不确定为什么会出现这种情况 :( - user3415167

1

如果您正在使用“实时数据库”或“Cloud Firestore”,请检查您的Angular NgModule和组件。请参阅使用“angularfire2”的差异:

实时数据库

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


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

云火存储。
@NgModule({
    ...
    imports: [
       AngularFirestoreModule,
       ...
    ]
})

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

1
因为您的方法查询的是 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());
            }
        }
    });

不要忘记这个:

不解释

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接