dataSnapshot无法获取父级推送键值

huangapple go评论79阅读模式
英文:

dataSnapshot could not get parent push key value

问题

我希望dataSnapshot能够检查其父级的summary中是否存在month”。但是dataSnapshot返回的结果是它在summary中没有month”。

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, "获得数值", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(newTransaction.this, "没有数值", Toast.LENGTH_LONG).show();
        }
    }
});

我的Firebase数据库我想要从其父级红线检查值蓝线)。
英文:

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

答案1

得分: 0

以下是代码的翻译部分:

String key = ds.getKey();

返回引用指向的节点的键此处为 `summary`。`summary` 节点和 `month` 属性之间您的结构中还有另一层实际上就是那个推送的键-MJKC ... xGZX)。为了检查该属性是否存在您还需要在引用中使用该键假设 `summary` 节点是您的 Firebase 数据库根节点的直接子节点请使用以下代码行

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, "获取到值", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(newTransaction.this, "没有值", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); // 不要忽略可能出现的错误!
    }
};
keyRef.addListenerForSingleValueEvent(valueEventListener);

然而您尚未将该键存储到变量中因此您应该使用查询如下所示

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, "获取到值", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(newTransaction.this, "没有值", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); // 不要忽略可能出现的错误!
    }
};
queryByMonth.addListenerForSingleValueEvent(valueEventListener);
英文:

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

huangapple
  • 本文由 发表于 2020年10月11日 09:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64299818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定