简体   繁体   English

如何在 Firebase 实时数据库中使用 firebase 唯一 uid 放置 UserDetails

[英]How to put UserDeatils using firebase unique uid in Firebase Realtime Database

I wanted to put a unique id every time when users signups into my apps.每次用户注册我的应用程序时,我都想设置一个唯一的 ID。 below is my code attached which is used to store data currently using vehicleNo:下面是我附加的代码,用于存储当前使用vehicleNo的数据:

FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference root = db.getReference("Drivers");
DriverModel driverModel = new DriverModel(name,contact,email,licenseNo,vehicleNo,age,bloodGroup,uri.toString(),drowsinessImage,dateTime);
root.child(vehicleNo).setValue(driverModel);

If the user is signed in with Firebase Authentication, you can get their UID with:如果用户使用 Firebase 身份验证登录,您可以通过以下方式获取其 UID:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

Then you can save the data under their UID with:然后,您可以使用以下方法将数据保存在其 UID 下:

DriverModel driverModel = new DriverModel(name,contact,email,licenseNo,vehicleNo,age,bloodGroup,uri.toString(),drowsinessImage,dateTime);
root.child(uid).setValue(driverModel);

You can get your key by using getKey() .您可以使用getKey()获取您的密钥。 Refer here: https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields请参阅此处: https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields

final String key = root.push().getKey();

Next, modify your model class DriverModel include the id .接下来,修改您的 model class DriverModel包括id Example:例子:

public class DriverModel{

    //Your others attributes...
    private String id;

    //Do get set also...

}

Last, when you want to store data.最后,当您想要存储数据时。 You can do like this.你可以这样做。

FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference root = db.getReference("Drivers");
final String key = root.push().getKey();
DriverModel driverModel = new DriverModel(key, name,contact,email,licenseNo,vehicleNo,age,bloodGroup,uri.toString(),drowsinessImage,dateTime);
root.child(vehicleNo).setValue(driverModel);

There are no error messages, simply because you cannot see them.没有错误消息,只是因为您看不到它们。 There is nothing in your code that handles errors.您的代码中没有任何内容可以处理错误。 To solve this, you have to attach a listener to the setValue() operation, to see if something goes wrong.要解决这个问题,您必须将侦听器附加到setValue()操作,以查看是否出现问题。 In code, it will be as simple as:在代码中,它将很简单:

root.child(vehicleNo).setValue(driverModel).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d("TAG", "Data successfully written.");
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

If you get an error message due to the fact that you have improper security rules, then go to your Firebase console and set them to allow the write operation .如果由于您的安全规则不正确而收到错误消息,则将 go 发送到您的 Firebase 控制台并将它们设置为允许写入操作

暂无
暂无

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

相关问题 如何使用 javascript 在 firebase 实时数据库中使用自定义 uid 进行身份验证? - how to authenticate with custom uid in firebase realtime database using javascript? Firebase 实时数据库通过 uid 检索数据并将其放入 Varable kotlin android - Firebase realtime database retrive data by uid and put it to Varable kotlin android 如何使用 Firebase 实时数据库制作部分并将图像放入表视图中 - How to make sections and put image in tableview using Firebase Realtime Database 如何写入firebase只允许某个UID编辑的实时数据库? - How to write to the firebase realtime database that only allows a certain UID to edit? How to Authenticate with Firebase realtime database using UID or Email and Password for REST API using postman - How to Authenticate with Firebase realtime database using UID or Email and Password for REST API using postman 如何在 firebase 实时数据库的安全规则中授予对 UID 列表的访问权限 - How to give access to a list of UID in security rule of firebase realtime database Firebase 身份验证和 Firebase 实时数据库中的 UID 不同 - The UID's in Firebase Authentication and Firebase Realtime Database are different 如何使用 Firebase 9(模块化 sdk)更新 Firebase 实时数据库中的数据 - How to update data in Firebase realtime database using Firebase 9 (modular sdk) firebase 实时数据库规则允许多个 uid 进行写访问 - firebase realtime database rules to allow multiple uid for write access 如何编写 firebase 实时数据库规则以匹配根文件夹子文件夹名称与新数据自定义 uid - how to write a firebase Realtime database rule to match root folder child folders name with New Data custom uid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM