Firebase permission-denied

2020-07-05 20:33发布

问题:

I'm a newbie in 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.

Currently, this is my code (which keeps returning 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 :)

回答1:

I was able to solve the problem by switching from Cloud Firestore to Realtime Database, and then changing the rule. (After I've changed this, there was no more error showing!)



回答2:

For others struggling with this problem, the proper solution is to change the false in

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

To true while testing. Don't forget to add additional security when going live with your project!



回答3:

You should check in your Angular NgModule and components if you are using "Realtime Database" or "Cloud Firestore". See diferences using "angularfire2":

REALTIME DATABASE

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


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

CLOUD FIRESTORE

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

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


回答4:

Because your method query the Firebase not Cloud Firestore so you should use this method :

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'