简体   繁体   English

向Firebase Realtime数据库添加数据时出现问题

[英]Issues adding data to Firebase Realtime database

So I'm having a strange issue with Firebase, and I can't seem to find any info on it. 所以我在Firebase中遇到一个奇怪的问题,而且我似乎找不到任何有关它的信息。 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. 当我检查Firebase时,它使用正确的用户名创建了一个对象,但没有添加任何字符串类型的属性,并且两个double属性均为零。 I've turned off user authentication for now, until I get this working. 我暂时关闭了用户身份验证,直到可以正常工作为止。 Here's the relevant code: 以下是相关代码:

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. 完成此操作后,如果我检查Firebase,它会显示“用户”,并带有一个名为“ testuser”的对象,但测试用户的唯一属性是身高和体重,均值为0。如果我将身高和体重更改为字符串而不是双精度,根本不添加任何内容。 As far as I can tell, everything is pretty much identical to the Firebase docs, so I'm pretty well stumped. 据我所知,所有内容都与Firebase文档几乎相同,因此我很困惑。 Anyone seen anything like this? 有人看到过这样的东西吗?

First, delete the static keyword from your User class declaration. 首先,从您的User类声明中删除static关键字。 Should only be: 只能是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM