我的数据库引用未显示在树中

Posted

技术标签:

【中文标题】我的数据库引用未显示在树中【英文标题】:My database reference does not show up in the tree 【发布时间】:2019-12-01 10:23:35 【问题描述】:

我有一个我创建的名为 getAssignedCustomerLocation 的方法,它使用变量名assignedCustomerRef 创建一个数据库引用,但是该数据库引用实际上并没有显示在我的firebase 控制台中。没有在实际控制台上为其创建节点。代码如下

 public void getAssignedCustomerLocation()
        String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference assignedCustomerRef = FirebaseDatabase.getInstance().getReference().child("customerRequest").child(userId).child("l");
        assignedCustomerRef.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
                if(dataSnapshot.exists())
                    List<Object> map = (List<Object>) dataSnapshot.getValue();
                    double locationLat = 0;
                    double locationLong =0;
                    if (map.get(0)!=null) 
                        locationLat= Double.parseDouble(map.get(0).toString());

                    
                    if (map.get(1)!=null)
                        locationLong = Double.parseDouble(map.get(1).toString());
                    
                    LatLng driverLatLong = new LatLng(locationLat,locationLong);
                    mMap.addMarker(new MarkerOptions().position(driverLatLong).title("your driver"));



                

            

【问题讨论】:

你能展示你的数据库结构吗? addValueEventListener 用于从 firebase db 读取,而不是写入。 请将您的数据库结构添加为 JSON 文件或至少作为屏幕截图。也请回复@AlexMamo 【参考方案1】:

您使用的代码是从 firebase 读取的。如果您想将新节点写入数据库,请使用以下内容。

String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference assignedCustomerRef = FirebaseDatabase.getInstance().getReference().child("customerRequest").child(userId).child("l");
assignedCustomerRef.setValue("YOUR_DATA");

【讨论】:

【参考方案2】:

要在数据库中创建节点需要使用setValue(),因此可以执行以下操作:

DatabaseReference assignedCustomerRef = FirebaseDatabase.getInstance().getReference().child("customerRequest").child(userId);
assignedCustomerRef.child("l").setValue("your driver");

【讨论】:

以上是关于我的数据库引用未显示在树中的主要内容,如果未能解决你的问题,请参考以下文章

在树节点中我想用路由计算最短路径

在树中的递归查找

如何检查二叉树的右侧。在树中搜索项目

JSON 数据未通过电话显示

c程序中特定函数的内存使用情况

如何在树中遍历并过滤javascript中的匹配节点?