简体   繁体   中英

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. 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:

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?

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

public class User {}

Add the no-argument constructor needed for Firebase.

public User() {}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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