Skip to content
Advertisement

dataSnapshot could not get parent push key value

I want dataSnapshot to check if “month” exists in its parent’s “summary”. But dataSnapshot is returning that it does not have “month” in “summary”

 ds = FirebaseDatabase.getInstance().getReference("summary");
 ds.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                //summary
                                String key = ds.getKey();
                                if (dataSnapshot.hasChild("month")) {

                                    Toast.makeText(newTransaction.this, "got value", Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(newTransaction.this, "No value", Toast.LENGTH_LONG).show();
                                }
                            }

My Firebase Database: I want to check the value (blue line) from its parent(red line)

My Firebase Database

Advertisement

Answer

The following line of code:

String key = ds.getKey();

Returns the key of the node on which the reference is pointing to, in this case summary. Between the summary node and the month property, there is another level in your structure, which actually is that pushed key (-MJKC … xGZX). In order to check if that property exists, you need to use that key in the reference too. Assuming that the summary node is a direct node of your Firebase database root, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference keyRef = rootRef.child("summary").child("-MJKC_JkVFpCdCGqxGZX");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.hasChild("month")) {
            Toast.makeText(newTransaction.this, "got value", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(newTransaction.this, "No value", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
    }
};
keyRef.addListenerForSingleValueEvent(valueEventListener);

However, you haven’t store that key into a variable, then you should use a query, as below:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference summaryRef = rootRef.child("summary");
Query queryByMonth = summaryRef.orderByChild("month")
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            Toast.makeText(newTransaction.this, "got value", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(newTransaction.this, "No value", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
    }
};
queryByMonth.addListenerForSingleValueEvent(valueEventListener);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement