为什么我的Firebase

为什么我的Firebase

本文介绍了为什么我的Firebase Realtime数据库仅保存1个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的实时数据库没有添加新数据,而是仅替换了新数据.必须添加而不是替换.

Why my Realtime Database is not adding a new data, instead it is replacing just a new data. It must be adding not replacing.

这是我添加新用户的代码:

This is my code for adding a new user:

public void addNewUser(String email, String username, String bio, String website, String interests, String address, String profile_photo){

    User user = new User(userID,1 , email, address, StringManipulation.condenseUsername(username));

    myRef.child(mContext.getString(R.string.dbname_users))
            .child(userID)
            .setValue(user);

    UserAccountSettings settings = new UserAccountSettings(
            interests,
            username,
            0,
            0,
            0,
            "",
            username,
            website,
            bio
    );

    myRef.child(mContext.getString(R.string.dbname_user_account_settings))
            .child(userID)
            .setValue(settings);
}

User类:

public class User {

private String user_id;
private long phone_number;
private String email;
private String address;
private String username;

public User(String user_id, long phone_number, String email, String address, String username) {
    this.user_id = user_id;
    this.phone_number = phone_number;
    this.email = email;
    this.address = address;
    this.username = username;
}

public User() {

}

public String getUser_id() {
    return user_id;
}

public void setUser_id(String user_id) {
    this.user_id = user_id;
}

public long getPhone_number() {
    return phone_number;
}

public void setPhone_number(long phone_number) {
    this.phone_number = phone_number;
}

public String getEmail() {
    return email;
}

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

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@Override
public String toString() {
    return "User{" +
            "user_id='" + user_id + '\'' +
            ", phone_number=" + phone_number +
            ", email='" + email + '\'' +
            ", address='" + address + '\'' +
            ", username='" + username + '\'' +
            '}';
}

UserAccountSettings类:

public class UserAccountSettings {

private String interests;
private String display_name;
private long followers;
private long truefans;
private long trustworthy;
private String profile_photo;
private String username;
private String website;
private String bio;

public UserAccountSettings(String interests, String display_name, long followers, long truefans, long trustworthy, String profile_photo, String username, String website, String bio) {
    this.interests = interests;
    this.display_name = display_name;
    this.followers = followers;
    this.truefans = truefans;
    this.trustworthy = trustworthy;
    this.profile_photo = profile_photo;
    this.username = username;
    this.website = website;
    this.bio = bio;
}

public UserAccountSettings() {

}

public String getInterests() {
    return interests;
}

public void setInterests(String interests) {
    this.interests = interests;
}

public String getDisplay_name() {
    return display_name;
}

public void setDisplay_name(String display_name) {
    this.display_name = display_name;
}

public long getFollowers() {
    return followers;
}

public void setFollowers(long followers) {
    this.followers = followers;
}

public long getTruefans() {
    return truefans;
}

public void setTruefans(long truefans) {
    this.truefans = truefans;
}

public long getTrustworthy() {
    return trustworthy;
}

public void setTrustworthy(long trustworthy) {
    this.trustworthy = trustworthy;
}

public String getProfile_photo() {
    return profile_photo;
}

public void setProfile_photo(String profile_photo) {
    this.profile_photo = profile_photo;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

public String getBio() {
    return bio;
}

public void setBio(String bio) {
    this.bio = bio;
}

@Override
public String toString() {
    return "UserAccountSettings{" +
            "interests='" + interests + '\'' +
            ", display_name='" + display_name + '\'' +
            ", followers=" + followers +
            ", truefans=" + truefans +
            ", trustworthy=" + trustworthy +
            ", profile_photo='" + profile_photo + '\'' +
            ", username='" + username + '\'' +
            ", website='" + website + '\'' +
            ", bio='" + bio + '\'' +
            '}';
}

这是我调用firebaseMethod.addNewUser的方法的地方:

This is where I call the method of firebaseMethod.addNewUser:

 mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());

                myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        // 1st check: Make sure the username is not already in use
                        if (firebaseMethods.checkIfUsernameExists(username, dataSnapshot)){
                            append = myRef.push().getKey().substring(3,10);
                            Log.d(TAG, "onDataChange: username already exists. Appending random string to name: " + append);
                        }

                        username = username + append;

                        // add new user to the database

                        firebaseMethods.addNewUser(email,username,"","","","","");

                        Toast.makeText(mContext, "Signup successful. Sending verification email.", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

                finish();

            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };

这是我在HomeActivity.class中的mAauth.signOut:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Log.d(TAG, "onCreate: starting.");

    setupFirebaseAuth();

    initImageLoader();
    setupBottomNavigationView();
    setupViewPager();

    FirebaseAuth.getInstance().signOut();


}

经过身份验证的用户的图片:在此处输入图片描述

Image for Authenticated users:enter image description here

数据库图片:在此处输入图片描述

推荐答案

Firebase允许将多个用户的信息存储在Firebase实时数据库中,但只能存储为经过身份验证的用户.我认为您尚未从第一个用户注销.这就是为什么,它仅节省了一个用户.您是否创建了退出按钮,并完成了以下操作:

Firebase allows to store information of multiple users in firebase realtime database but only as authenticated users. I think you have not signout from your first user. That is why, its saving only one user. Have you created a signout button and have you done something like this :

FirebaseAuth mAuth = FirebaseAuth.getInstance();
mAuth.signOut();

这篇关于为什么我的Firebase Realtime数据库仅保存1个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:44