dataSnapshot无法获取父推键值 [英] dataSnapshot could not get parent push key value

查看:41
本文介绍了dataSnapshot无法获取父推键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望dataSnapshot检查"month"是否为存在于其父公司的摘要"中.但是dataSnapshot返回的是它没有"month"在摘要"中

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

我的Firebase数据库:我想检查其父级(红线)中的值(蓝线)

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

我的Firebase数据库

推荐答案

以下代码行:

String key = ds.getKey();

返回引用所指向的节点的键,在这种情况下为summary.在summary节点和month属性之间,结构中还有另一个层次,实际上是该推键(-MJKC ... xGZX).为了检查该属性是否存在,您还需要在引用中使用该键.假设summary节点是Firebase数据库根目录的直接节点,请使用以下代码行:

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

这篇关于dataSnapshot无法获取父推键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆