Firebase为什么认为密钥包含。设置新值时?

时间:2019-03-06 05:37:34

标签: java android firebase firebase-realtime-database firebase-authentication

我正在尝试使用android-quickstart教程中概述的确切方法将对象插入Firebase。但是,当我尝试使用密钥uid添加新用户时,会引发异常:

com.google.firebase.database.DatabaseException: Invalid key: . Keys must not contain '/', '.', '#', '$', '[', or ']'
        at com.google.firebase.database.core.utilities.Validation.validateWritableKey(com.google.firebase:firebase-database@@16.1.0:120)
        at com.google.firebase.database.core.utilities.Validation.validateWritableObject(com.google.firebase:firebase-database@@16.1.0:103)
        at com.google.firebase.database.core.utilities.Validation.validateWritableObject(com.google.firebase:firebase-database@@16.1.0:104)
        at com.google.firebase.database.DatabaseReference.setValueInternal(com.google.firebase:firebase-database@@16.1.0:293)
        at com.google.firebase.database.DatabaseReference.setValue(com.google.firebase:firebase-database@@16.1.0:166)
        ....

在此之前运行的代码是:

/* FirebaseUser is created with emailAndPassword just before this  and the
 * uid is retrieved from that new user.
 */
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
db = db.child("users");
db = db.child(uid);
Log.d(TAG, "Attempting to insert user at" + uid );
Log.d(TAG, db.toString());
db.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d(TAG, "User created at " + uid);
        } else {
            Log.w(TAG, task.getException());
        }
    }
});

在发生异常之前的输出是:

D/FirebaseAuth: Notifying id token listeners about user ( gDGy7BIGvQWjnQ7yZVCsdkIbAzx1 ).
D/SignUpActivity: Attempting to insert user at gDGy7BIGvQWjnQ7yZVCsdkIbAzx1
D/SignUpActivity: https://my-app.firebaseio.com/users/gDGy7BIGvQWjnQ7yZVCsdkIbAzx1
D/FirebaseAuth: Notifying auth state listeners about user ( gDGy7BIGvQWjnQ7yZVCsdkIbAzx1 ).

FirebaseUser的uid显然没有任何非法字符,因此我不确定该异常来自何处。

编辑: user是一个User对象,其中电子邮件和用户名字段已初始化,而其他一些尚未初始化的字段(名称和位置)尚未实现。此setValue()操作正在代码的先前迭代中进行。在当前的迭代中,User还实现了Parcelable,以便可以在活动之间传递User。

当前用户声明:

public class User implements Parcelable {
    private String username;
    private String email;
    private String name;
    private Location location;

    public User() {}
        public User(String username, String email) {
        this.username = username;
        this.email = email;
    }

    protected User(Parcel in) {
        username = in.readString();
        email = in.readString();
        name = in.readString();
        location = in.readParcelable(Location.class.getClassLoader());
    }

    public static final Creator<User> CREATOR = new Creator<User>() { ... };

        public User(String username, String email) {
        this.username = username;
        this.email = email;
    }

    protected User(Parcel in) {
        username = in.readString();
        email = in.readString();
        name = in.readString();
        location = in.readParcelable(Location.class.getClassLoader());
    }

1 个答案:

答案 0 :(得分:0)

从我最近的编辑中可能会很清楚,但是似乎导致实现此问题的原因是实施Parcelable并尝试读取/写入未初始化的字段。如果我从Parcelable实现中删除了未初始化的字段,则setValue()会按预期工作。

    nodo
      .attr("width", function(d) {
        return d.x*3.4;
      })
      .attr("height", function(d) {
        return d.y*3.4;
      });