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

时间:2022-02-08 06:56:36

So I'm having a strange issue with Firebase, and I can't seem to find any info on it. I'm attempting to add a user object to the database. When I check Firebase, it's created an object with the correct username, but it doesn't add any of the attributes that are Strings, and the 2 double attributes are zero. I've turned off user authentication for now, until I get this working. Here's the relevant code:

所以我对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"));

After that completes, if I check Firebase, it shows "users", with an object called "testuser", but the testuser's only attributes are height and weight, both with value 0. If I change height and weight to Strings instead of doubles, nothing at all is added to the database. As far as I can tell, everything is pretty much identical to the Firebase docs, so I'm pretty well stumped. Anyone seen anything like this?

完成之后,如果我检查Firebase,它会显示“users”,其中包含一个名为“testuser”的对象,但testuser的唯一属性是height和weight,值均为0.如果我将高度和重量更改为Strings而不是double,什么都没有添加到数据库中。据我所知,一切都与Firebase文档完全相同,所以我很难过。有人见过这样的事吗?

1 个解决方案

#1


0  

First, delete the static keyword from your User class declaration. Should only be:

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

public class User {}

Add the no-argument constructor needed for Firebase.

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

public User() {}

Add also the getters and setters for all of your fields.

添加所有字段的getter和setter。

This how your model class should look like:

这个模型类应该是这样的:

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

#1


0  

First, delete the static keyword from your User class declaration. Should only be:

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

public class User {}

Add the no-argument constructor needed for Firebase.

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

public User() {}

Add also the getters and setters for all of your fields.

添加所有字段的getter和setter。

This how your model class should look like:

这个模型类应该是这样的:

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