简体   繁体   English

使用Firebase Android API通过嵌套数据排序查询时发生异常

[英]Exception when ordering query by nested data with Firebase Android API

According to the Firebase documentation, it should be possible to order a query by a deeply nested child by passing the full path to the orderByChild() method like this 根据Firebase文档,应该可以通过将完整路径传递给orderByChild()方法来对深度嵌套的子项进行查询排序

Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
Query queryRef = ref.orderByChild("dimensions/height");
queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
        DinosaurFacts facts = snapshot.getValue(DinosaurFacts.class);
        System.out.println(snapshot.getKey() + " was " + facts.getHeight() + " meters tall");
    }
});

But calling orderByChild("dinosaurs/height") throws a FirebaseException with the message Invalid key: dinosaurs/height. Keys must not contain '/', '.', '#', '$', '[', or ']' 但是,调用orderByChild("dinosaurs/height")会引发FirebaseException ,并显示消息Invalid key: dinosaurs/height. Keys must not contain '/', '.', '#', '$', '[', or ']' Invalid key: dinosaurs/height. Keys must not contain '/', '.', '#', '$', '[', or ']' . Invalid key: dinosaurs/height. Keys must not contain '/', '.', '#', '$', '[', or ']'

Indeed, the key is rejected by the following firebase method 实际上,该密钥被以下firebase方法拒绝

private static final Pattern INVALID_KEY_REGEX = Pattern.compile("[\\[\\]\\.#\\$\\/\\u0000-\\u001F\\u007F]");

private static boolean isValidKey(String key) {
    return key.equals(".info") || !INVALID_KEY_REGEX.matcher(key).find();
}

What is the correct way of querying using the full path of an object? 使用对象的完整路径进行查询的正确方法是什么?

The ability to order by a nested child was added in version 2.4 of the Firebase SDK for Java . Firebase SDK for Java 2.4版增加了通过嵌套子项进行订购的功能。

I just tried using a / on an older version of the SDK, and get the same error as you have. 我只是尝试在旧版SDK上使用/ ,并得到与您相同的错误。 So my best bet is that you forgot to update your project to use Firebase SDK version 2.4. 因此,我最好的选择是,您忘记了更新项目以使用Firebase SDK 2.4版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM