【问题标题】:How to retrieve the data that inserted in the map firebase fire store如何检索插入到地图 Firebase Firestore 中的数据
【发布时间】:2021-07-15 16:04:00
【问题描述】:

我正在 android studio IDE 中创建一个应用程序,我想在文本框中显示插入的数据 但问题是我不知道如何检索我在驱动程序字段中插入的数据。驱动字段是一个映射,有没有办法检索驱动字段中插入的数据?这是我的firebase fire store 的样子,我想获取三轮车号码数据。 谁能给我举例说明如何在地图集合字段中检索数据?

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    按照getting data from Firestore 上的文档,您可以获得该位置数据的DataSnapshot 对象。然后使用DocumentSnapshot#get() 获取单个字段的值。

    这可以使用:

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    
    DocumentReference driverDocRef = db.collection("Driver Locations")
        .document(driverId);
    
    driverDocRef.get()
        .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + document.get("driver.tricyclenumber", String.class));
                    } else {
                        Log.d(TAG, "No such document");
                    }
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
    

    要将其变成可以在其他地方重复使用的函数,您可以使用Task#onSuccessTask() 将任务链接在一起。

    一个这样的实现是:

    public Task<String> getDriverTricycleNumber(String driverId) {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
    
        DocumentReference driverDocRef = db.collection("Driver Locations")
            .document(driverId);
    
        return driverDocRef.get()
            .onSuccessTask(new SuccessContinuation<DocumentSnapshot, String>() {
                @NonNull
                @Override
                public Task<String> then(DocumentSnapshot document) {
                    if (!document.exists()) {
                        throw new DriverNotFoundException(); // <-- a custom exception of your choosing
                    }
                    return document.get("driver.tricyclenumber", String.class);
                }
            });
    }
    
    // to use:
    getDriverTricycleNumber("someDriverId")
        .addOnCompleteListener(new OnCompleteListener<Number>() {
            @Override
            public void onComplete(@NonNull Task<Number> task) {
                if (task.isSuccessful()) {
                    String tricycleNumber = task.getResult();
                    Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + tricycleNumber);
                } else {
                    Log.d(TAG, "Couldn't get tricycle number", task.getException());
                }
            }
        });
    

    注意:(可选)您可以使用现代箭头符号和链接来简化上述代码:

    public Task<String> getDriverTricycleNumber(String driverId) {
        return FirebaseFirestore.getInstance()
            .collection("Driver Locations")
            .document(driverId)
            .get()
            .onSuccessTask(document -> {
                if (!document.exists()) {
                    throw new DriverNotFoundException(); // <-- a custom exception of your choosing
                }
                return document.get("driver.tricyclenumber", String.class);
            });
    }
    
    getDriverTricycleNumber("someDriverId")
        .addOnSuccessListener(tricycleNumber -> {
            Log.d(TAG, "Driver #" + driverId + "'s Tricycle Number is " + tricycleNumber);
        })
        .addOnFailureListener(exception -> {
            Log.d(TAG, "Couldn't get tricycle number", exception);
        });
    

    【讨论】:

    • yo @samthecodingman 感谢您提供的示例。真的很感激。
    • @samthecodingman tricyclenumber 字段是一个字符串,所以我在您的代码中相应地更改了它。
    • @AlexMamo 你错过了一些地方,但现在它们也得到了纠正。然而,我的印象是 OP 使用了错误的类型,但即便如此,Firebase 还是会强制他们使用数字。
    • 是的,Task&lt;String&gt; 也是。我错过了。谢谢。
    【解决方案2】:

    如果是地图,则只需使用完全标准的点表示法:

    const myFirstname = data.driver.firstname;
    const myLastname = data.driver.lastname
    etc etc etc
    

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2022-11-01
      • 2023-03-09
      • 2021-02-16
      • 2020-08-08
      • 1970-01-01
      相关资源
      最近更新 更多