Firebase 无效键:。密钥不得包含"/"、"."、"#"、"$"、"["或"]"



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

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();
    }
});

以下是动物数据密钥在更新之前是如何出现的儿童:

"动物/-Kcd_8Tif5EPYUhsceeH">

"users-animals/LoQ9Bkjs2yVC95nFGyo1ft4cqdB2/-Kcd_8Tif5EPYUhsceeH">

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

不确定

这是否会对您有所帮助,但我正在使用一些与我的个人对象相适应的组件:D(例如,Primeng自动选择框向我的数据添加了类似"_$visited"的内容(。如果有人面临同样的问题,并且您的对象中并不真正需要这些键,则可以考虑调用以下函数。(你需要有洛达什(

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;
}

最新更新