简体   繁体   中英

Firebase Database Permission denied on 2 root child

There are my database rules:

    "Results" : {
      ".indexOn":["namaGroup"],
        ".read": "auth != null",
          ".write": "auth !=null",
    },

And it's my code:

    private void updateScore(final String namaGroup, final RankingCallback<Ranking> callback) {
            questionScore = FirebaseDatabase.getInstance().getReference();
            final DatabaseReference ref1= questionScore.child("Results");
            ValueEventListener valueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for (DataSnapshot ds:snapshot.getChildren()) {
                        String key = ds.getKey();
    
                        DatabaseReference ref = questionScore.child(key);
                        ref.orderByChild("namaGroup").equalTo(namaGroup);
                        ValueEventListener eventListener = new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                for (DataSnapshot ds : snapshot.getChildren()) {
                                        GroupScore ques = ds.getValue(GroupScore.class);
                                        sum += Integer.parseInt(String.valueOf(Objects.requireNonNull(ques).getScore()));
    
                                }
                                Ranking ranking = new Ranking(namaGroup, sum);
                                callback.callBack(ranking);
                                Log.d("TAG", ranking.getNamaGroup());
                                Log.d("TAG", String.valueOf(ranking.getScore()));
                            }
    
                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {
    
                            }
                        };
                        ref.addListenerForSingleValueEvent(eventListener);
                    }
    
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
    
                }
            };
            ref1.addListenerForSingleValueEvent(valueEventListener);

I get the next error as the warning log: DatabaseError: Permission denied .

I want to sum a score of each member by looping a snapshot like my code from table results: Structure of the database

Try this rules

"Results" : {
  ".indexOn":["namaGroup"],
    ".read": true,
      ".write": "auth!=null",
},

Hope this helps Feel free to ask for clarifications...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM