Firebase存储用户变量? [英] Firebase store user variables?

查看:124
本文介绍了Firebase存储用户变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase作为我的初学者项目,而且我对提供的文档感到困惑。我已经初始化一个认证表单,它正在工作。但我不明白如何将它链接到实时数据库?我知道(用户)是当前记录它的用户,但我如何将信息存储在目前的用户?



在我的应用程序中,每个用户都有一个余额(例如$ 100)。如何将用户的余额存储在他们的用户名中,我是否需要使用实时数据库?

解决方案

基本上,
当新用户注册时,您可以通过 user.uid 访问其UID,您可以使用该UID将信息存储在实时数据库中。



您可以创建一个这样的结构。

  root:{
users :{
$ uid:{
//将您的所有自定义信息存储在此处。
余额:100
}
}
}

对于每个用户,您都要将自己的信息写入users / uid。这应该使保存和获取信息相当微不足道。

为了获得当前用户的余额,所有你需要做的是观察users / uid / balance



创建一个用户。

  firebase.auth()。createUserWithEmailAndPassword(email,password)
.then(function(user){
alert(user.uid)
})。catch(function错误){
console.log(error.message)
});

读取和写入数据


I am using Firebase for a beginner project of mine, and I am confused by the documentation that is provided. I have initialized an authentication form, and it's working. But I do not understand how to link it to the realtime database?

I understand that (user) is the currently logged it user, but how do I store information on the current user?

In my app, each user has a Balance(eg $100). How do I store the user's balance in their username, do I need to use the realtime databse?

解决方案

Basically, When a new user registers you'll get access to his UID through user.uid, you can use that UID to store info in the realtime database.

You can create a structure like this.

root: {
  users: {
    $uid: {
      // Store all your custom info here.
      balance: 100
    }
  }
}

And for every user you'd write their info to "users/uid". That should make saving and fetching info fairly trivial.

And to get the current user's balance all you'd have to do is observe "users/uid/balance".

Create an user.

firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function(user){
  alert(user.uid)
}).catch(function(error) {
    console.log(error.message)
});

Reading and writing data

这篇关于Firebase存储用户变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆