![](/img/trans.png)
[英]How to add previous data with new one in firebase realtime database javascript
[英]How to add data in firebase database
我正在创建停车场应用程序,我希望用户在注册前在编辑文本中输入一些信息。 编辑文本如下:
First Name
, Last Name
, Email
, password
, car no
。
当用户点击注册按钮时,我想将这些值存储在连接到我的项目的 firebase 数据库中。我想知道如何在 firebase 中创建表以及如何存储这些值。 我是编程新手。
首先检索编辑文本:
String email = textEmail.getText().toString().trim();
String firstname = firstName.getText().toString().trim();
//etc
首先使用createUserWithEmailAndPassword
验证用户,然后添加到数据库:
private DatabaseReference mDatabase, newUser;
private FirebaseUser mCurrentUser;
mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toasty.info(getApplicationContext(), "creation of account was: " + task.isSuccessful(), Toast.LENGTH_SHORT).show();
if (!task.isSuccessful()) {
Toasty.error(getApplicationContext(), "Authentication failed: " + task.getException().getMessage(),
Toast.LENGTH_SHORT).show();
} else {
mCurrentUser= task.getResult().getUser();<-- gets you the uid
newUser=mDatabase.child(mCurrentUser.getUid());
newUser.child("email").setValue(email);
newUser.child("firstname").setValue(name);
}
});
您将拥有以下数据库:
users
userid
email:userx@gmail.com <--example
firstname:userx <--example
Firebase 不将数据存储在表中,它不是经典数据库。 它只是一个大的 JSON 文件,可能会在多个节点中复制,也可能跨越其中的几个节点。
经典方法是让根的第一级子级成为您想象的表。 因此,例如,您将拥有
{
"users": {
...
}
}
其次,你要做的事情很简单,打开入门指南就知道怎么做了。
此外,处理用户身份验证的最佳方法不是这个,而是使用 FirebaseAuth。 在指南中,您也会找到有关此内容的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.