Firebase permission-denied

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 :)

Answers


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!)


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!


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();
     }
...
}

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'

Need Your Help

Something Better than .NET Reflector?

.net reflector redgate

I used to love Reflector back in the day, but ever since RedGate took over it has gone downhill dramatically. Now it forces me to update (which is absolutely ridiculous), half the time the update ...

How to access the value of a promise?

javascript angularjs promise angular-promise

I'm looking at this example from Angular's docs for $q but I think this probably applies to promises in general. The example below is copied verbatim from their docs with their comment included: