向Firebase Realtime数据库添加数据的问题

时间:2018-02-20 18:19:48

标签: java android firebase firebase-realtime-database

所以我对Firebase有一个奇怪的问题,而我似乎无法找到任何相关信息。我试图将用户对象添加到数据库中。当我检查Firebase时,它创建了一个具有正确用户名的对象,但它没有添加任何属性为Strings,并且2个double属性为零。我暂时关闭了用户身份验证,直到我开始工作。这是相关代码:

public class User {

    public String email;
    public String password;
    public String firstName;
    public String lastName;
    public String dob;
    public double height;
    public double weight;
    public String gender;

    public User()
    {

    }

    public User(String email, String password, String firstName, String lastName, String DoB, double height, double weight, String gender) {

    }

    public String getEmail()
    {
        return email;
    }

    public String getPassword()
    {
        return password;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public String getdob()
    {
        return dob;
    }

    public double getHeight()
    {
        return height;
    }

    public double getWeight()
    {
        return weight;
    }

    public String getGender()
    {
        return gender;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public void setdob(String dob)
    {
        this.dob = dob;
    }

    public void setHeight(double height)
    {
        this.height = height;
    }

    public void setWeight(double weight)
    {
        this.weight = weight;
    }

    public void setGender(String gender)
    {
        this.gender = gender;
    }
}

final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference usersRef = database.getReference();

...

private void registerUser() {

    usersRef.child("testuser").setValue(new User("test@test.com", "password", "First", "Last", "2000/01/01", 172, 75, "Male"));

完成后,如果我检查Firebase,它会显示" users",并显示一个名为" testuser"的对象,但testuser的唯一属性是身高和体重,值为0.如果我将高度和重量更改为字符串而不是双精度数,则不会向数据库添加任何内容。据我所知,一切都与Firebase文档完全相同,所以我很难过。有人见过这样的事吗?

1 个答案:

答案 0 :(得分:0)

首先,从static类声明中删除User关键字。应该只是:

public class User {}

添加Firebase所需的无参数构造函数。

public User() {}

还为所有字段添加getter和setter。

这样你的模型类应如何:

public class User {
    private String email;
    private String password;
    private String firstName;
    private String lastName;
    private String dob;
    private double height;
    private double weight;
    private String gender;

    public User() {}

    public User(String email, String password, String firstName, String lastName, String dob, double height, double weight, String gender) {
        this.email = email;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.dob = dob;
        this.height = height;
        this.weight = weight;
        this.gender = gender;
    }

    public String getEmail() {return email;}

    public String getPassword() {return password;}

    public String getFirstName() {return firstName;}

    public String getLastName() {return lastName;}

    public String getDob() {return dob;}

    public double getHeight() {return height;}

    public double getWeight() {return weight;}

    public String getGender() {return gender;}
}