2
votes

I was trying to store data to Firebase Cloud Firestore but after running my app it showed this error in the log.

[Firestore]: Write failed at Backpaper Registration/WSjZoirLf8WRBuGCVbUZ: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

This is the part of my code.

fb=FirebaseFirestore.getInstance();

map=new HashMap<String>();
        map.put("Name of Department",txtDepartment);
        map.put("Name of Student",txtStudentName);
        map.put("Registration No",txtRegNo);
        map.put("CGPA in Previous Semester",txtCgpa);
        map.put("Phone",txtPhoneNo);
        map.put("Email",txtEMail);
        map.put("Subject 1",txtSubject1);
        map.put("Subject 2",txtSubject2);
        map.put("Subject 3",txtSubject3);
        map.put("Subject 4",txtSubject4);
        map.put("Subject 5",txtSubject5);
        map.put("Payment Details(Rs.)",txtPaymentDetails);
        map.put("Payment Date",txtPaymentDate);

        fb.collection("Backpaper Registration").add(map).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
            @Override
            public void onComplete(@NonNull Task<DocumentReference> task) {
                if(task.isSuccessful()){
                    Toast.makeText(Backpaper.this, "Registration Successful", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(Backpaper.this,Academic.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
                    finish();

                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(Backpaper.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

Can somebody help me?

3
Have you switched Firebase Database to Test Mode ?Hritik Gupta

3 Answers

1
votes

You should check out the Rules on Firebase

Change allow read, write: if false; to true;

Nots:

It is not advised to choose this solution because it makes the database unsafe.

Only use this for testing purposes.

1
votes

You need the Firebase rules setting at Firebase console.

If you had signed into project, the rules setting may like :

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

If you just demo the function, you call setting the rules:

// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

The more officially document you can found here.

https://firebase.google.com/docs/firestore/security/get-started

0
votes

It could be because, you have not given the read & write permission to firebase.
Change your Firebase Database to test mode.

1. Go to your firebase database console
2. Go to Rules
3. Start in test mode
This will change allow read, write: true

Firestore, Start in **test mode** This turn off the database security but for testing purpose it is advised