11

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.

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user3415167
  • 1,013
  • 2
  • 13
  • 23

4 Answers4

38

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!) enter image description here

user3415167
  • 1,013
  • 2
  • 13
  • 23
3

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!

Nathan Bird
  • 910
  • 1
  • 9
  • 23
  • I've also tried that, but still got permission denied. Even though I've solved the problem in another way, I'm not sure why for this :( – user3415167 Dec 26 '17 at 23:02
1

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();
     }
...
}
Matjaz Hirsman
  • 326
  • 3
  • 9
1

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'
Evan Ngo
  • 254
  • 4
  • 15