【问题标题】:Firebase Invalid key: . Keys must not contain '/', '.', '#', '$', '[', or ']'Firebase 无效密钥: 。键不能包含“/”、“.”、“#”、“$”、“[”或“]”
【发布时间】:2017-02-10 18:19:35
【问题描述】:

为什么会发生这个错误?在调试模式下,键中没有特殊字符,没有“.”,只有路径必需的“/”。它运行良好,我只是擦除了我的数据库,然后我再次运行标题中的错误。我的代码:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

String animalUid = animal.getUid();

if (animalUid == null) {
    animalUid = databaseReference.push().getKey();
}

Map<String, Object> animalData = new HashMap();


if(animal.getFavorites()!=null) {
    for (Map.Entry<String, Boolean> entry : animal.getFavorites().entrySet()) {
        animalData.put("users-favorites-animals/" + entry.getKey() + "/" + animalUid, animal);
    }
}

animalData.put("users-animals/" + animal.getOwnerUid() + "/" + animalUid, animal);
animalData.put("animals/" + animalUid, animal);

databaseReference.updateChildren(animalData).addOnCompleteListener(new OnCompleteListener() {
    @Override
    public void onComplete(@NonNull Task task) {
        listener.onSaveAnimalSucess(animal);
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {

        if (e instanceof FirebaseException) {
            listener.onSaveAnimalError("");
            return;
        }

        listener.onConnectionError();
    }
});

在 updateChildren 之前,animalData 键是如何出现的:

“动物/-Kcd_8Tif5EPYUhsceeH”

“用户-动物/LoQ9Bkjs2yVC95nFGyo1ft4cqdB2/-Kcd_8Tif5EPYUhsceeH”

即使我没有多数据更新的旧代码也无法正常工作,并出现同样的错误。我不知道发生了什么。

【问题讨论】:

  • 密钥不应包含 /,您的密钥包含。这里有什么不清楚的地方?
  • 这不是密钥,这是 firebase 支持的深度路径,但它一直将密钥作为旧的 firebase 版本调用。不知道为什么firebase.googleblog.com/2015/09/…
  • 您正在阅读旧版 firebase (Android (2.4.0)) 的博客。最新的在哪里10.0.x...查看最新的API
  • 这是多路径更新,我认为他们没有从 Firebase 中删除此功能。看这个视频的日期youtube.com/watch?v=i1n9Kw3AORw
  • 你能告诉我们animalUidanimal.getOwnerUid()的值吗?

标签: android firebase


【解决方案1】:

不确定这是否会对您有所帮助,但我正在使用一些组件来处理我的个人对象:D(例如,primeng 自动选择框会在我的数据中添加类似“_$visited”的内容)。 如果有人遇到同样的问题并且你的对象中并不真正需要这些键,你可以考虑调用以下函数。 (你需要有 lodash)

private makeObjectGreatAgain(object: any) {
// removing undefined values from any arrays!
// and some variables which are added by different components
// firebase not allowed keys: ".", "#", "$", "/", "[", or "]"
object = JSON.parse(JSON.stringify(object), (key, val) => {
  if (!_.includes(key, '.') && !_.includes(key, '#') && !_.includes(key, '$') &&
    !_.includes(key, '/') && !_.includes(key, '[') && !_.includes(key, ']')) {
    return val;
  } else {
    console.log('removing invalid key: ' + key + ' val: ' + val);
  }
});
return object;
}

【讨论】:

    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2018-08-24
    • 1970-01-01
    • 2019-04-30
    相关资源
    最近更新 更多