, '[', Or ']' Why is this error occurring? In debug mode, there are no special ...">
, '[', Or ']' - android ▫️ 🧒🏿 💇🏾

Firebase Invalid Key :. Keys must not contain '/', '.', '#', '$', '[', Or ']'

Why is this error occurring? In debug mode, there are no special characters in the key, no ".", Only "/" needed for the path. It worked well, I just destroyed my database and then ran the error in the name again. My code is:

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

This is how animalData keys appear before updateChildren:

"animals / -Kcd_8Tif5EPYUhsceeH"

"animal users / LoQ9Bkjs2yVC95nFGyo1ft4cqdB2 / -Kcd_8Tif5EPYUhsceeH"

Even my old code without updating multiple data does not work with the same error. I have no idea what is going on.

+4
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;
}
+1

Source: https://habr.com/ru/post/1669407/


All Articles