将新用户添加到firebase数据库android

时间:2018-04-04 11:57:07

标签: android firebase firebase-realtime-database

这是我向数据库添加新用户的方法我不知道我做错了什么

//add a new user

public void addNewUser(String email, String username, String description, String website, String profile_photo) {
    User user = new User(email, 1, userID,StringManipulations.condenseUsername(username));
    myRef.child(mContext.getString(R.string.dbName_user))
                .child(userID)
                .setValue(user);

    User_account_settings settings = new User_account_settings(description,
                username,
                0,
                0,
                0,
                profile_photo,
                StringManipulations.condenseUsername(username),
                website
     );
    myRef.child(mContext.getString(R.string.dbName_user_account_setting))
                .child(userID)
                .setValue(settings);

}

以下是节点的firebase数据库设置:

click to open image

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试添加Firebase.CompletionListener(),如果出现错误,您将获得错误,并且成功后会显示成功消息。

  //add a new user

public void addNewUser(String email, String username, String description, String website, String profile_photo) {
    User user = new User(email, 1, userID,StringManipulations.condenseUsername(username));
    myRef.child(mContext.getString(R.string.dbName_user))
                .child(userID)
                .setValue(user, new Firebase.CompletionListener() {
    @Override
    public void onComplete(FirebaseError firebaseError, Firebase firebase) {
        if (firebaseError != null) {
            System.out.println("Data could not be saved. " + firebaseError.getMessage());
        } else {
            System.out.println("Data saved successfully.");
        }
    }
});

    User_account_settings settings = new User_account_settings(description,
                username,
                0,
                0,
                0,
                profile_photo,
                StringManipulations.condenseUsername(username),
                website
     );
    myRef.child(mContext.getString(R.string.dbName_user_account_setting))
                .child(userID)
                .setValue(settings, new Firebase.CompletionListener() {
    @Override
    public void onComplete(FirebaseError firebaseError, Firebase firebase) {
        if (firebaseError != null) {
            System.out.println("Data could not be saved. " + firebaseError.getMessage());
        } else {
            System.out.println("Data saved successfully.");
        }
    }
});

}