0

Why I cant add data into firestore? It tell [Firestore]: Write failed at Menu/10UDGSouKFeCmJ7oImKDkEbfD0F2: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null} W/Menu: Error writing document com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.

public class AddMenu extends AppCompatActivity implements View.OnClickListener {

    EditText Name, Desc, Type, Price, Quantity;
    Button AMMenu;
    private static final String TAG = "Menu";
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference userRef = db.collection("Menu");
    private ProgressDialog progressDialog;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_menu);

        StorageReference mStorageRef;
        mStorageRef = FirebaseStorage.getInstance().getReference();
        firebaseAuth = FirebaseAuth.getInstance();
        progressDialog = new ProgressDialog(this);

        Name = (EditText) findViewById(R.id.SetName);
        Type = (EditText) findViewById(R.id.Type);
        Price = (EditText) findViewById(R.id.Price);
        AMMenu = (Button) findViewById(R.id.AMButton);
        Quantity = (EditText) findViewById(R.id.Quantity);
        Desc = (EditText) findViewById(R.id.Desc);

        AMMenu.setOnClickListener(this);
    }

    private void AddMenu() {
        final String name = Name.getText().toString().trim();
        final String type = Type.getText().toString().trim();
        final String price = Price.getText().toString().trim();
        final String quantity = Quantity.getText().toString().trim();
        final String desc = Desc.getText().toString().trim();

        if (TextUtils.isEmpty(name)) {
            //name is empty
            Toast.makeText(this, "Please enter Menu Set Name", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(type)) {
            //username is empty
            Toast.makeText(this, "Please enter Menu Type", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(price)) {
            //password is empty
            Toast.makeText(this, "Please enter Menu Price", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(quantity)) {
            //name is empty
            Toast.makeText(this, "Please enter Quantity", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(desc)) {
            //name is empty
            Toast.makeText(this, "Please enter Description", Toast.LENGTH_SHORT).show();
            return;
        }

        //if validation are ok
        //we will first show a progress bar

        progressDialog.setMessage("Adding Menu....");
        progressDialog.show();

        firebaseAuth = FirebaseAuth.getInstance();

        Log.d(TAG, "onComplete: "+ firebaseAuth.getUid());

        Map<String, Object> Project = new HashMap<>();
        Project.put("Set Name", name);
        Project.put("Description", desc);
        Project.put("Type", type);
        Project.put("Price", price);
        Project.put("Quantity", quantity);


        userRef.document(firebaseAuth.getUid()).set(Project)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "DocumentSnapshot successfully written!");
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w(TAG, "Error writing document", e);
                    }
                });

        Toast.makeText(AddMenu.this, "Saved Successfully", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(AddMenu.this, AddMenu.class);
        startActivity(intent);


    }

    @Override
    public void onClick(View view) {
        if (view == AMMenu){
            AddMenu();
        }
    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

0 Answers0