4

Why is this error happening? In the debug mode there's no special characters in the key, no '.', only the '/' necessary for the path. It was working well, I just wiped my database and then I run again with the error in the title. My code:

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

Here how the animalData keys comes before updateChildren:

"animals/-Kcd_8Tif5EPYUhsceeH"

"users-animals/LoQ9Bkjs2yVC95nFGyo1ft4cqdB2/-Kcd_8Tif5EPYUhsceeH"

Even my old code without the multi-data updates is not working, with the same error. I have no idea what's going on.

9
  • 3
    Key should not contain /, your key does. What is not clear here? Feb 10, 2017 at 18:23
  • 1
    this is not a key, this is a deep path that firebase supports, but it keeps calling key as an old firebase version. I don't know why firebase.googleblog.com/2015/09/… Feb 10, 2017 at 18:38
  • 1
    You are reading a blog from an old version of firebase (Android (2.4.0)). Where the latest is 10.0.x... Check the latest API Feb 10, 2017 at 18:48
  • This is Multi-path updates ,I don't think they removed this function from Firebase. Look the date of this video youtube.com/watch?v=i1n9Kw3AORw Feb 10, 2017 at 19:09
  • Can you please tell us the values of animalUid, and animal.getOwnerUid()? Feb 10, 2017 at 19:32

1 Answer 1

1

not sure if that will help you, but i am using some components which temper with my personal objects :D (for example, primeng autoselect box adds something like '_$visited' to my data). In case someone is facing the same issue and does not really need those kind of keys in your objects you might consider calling the following function. (you need to have lodash for it)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.