【问题标题】: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

标签: java android firebase firebase-realtime-database geofire


【解决方案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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 2018-07-06
      相关资源
      最近更新 更多