2

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?

ThorNiLs
  • 55
  • 2
  • 7
  • Yes, you need to use the realtime database. Here's how you read and write data. https://firebase.google.com/docs/database/web/read-and-write –  Mar 11 '17 at 13:03
  • Okay, thanks. But what the documentation isn't telling me is how I should layout the database for it to work with authentication, I feel like there's a huge chunk missing in there. – ThorNiLs Mar 11 '17 at 13:39

1 Answers1

3

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

  • Thank you, that makes sense! But I can't understand why my databse isn't updating... https://gyazo.com/22556e96d9e8e405f9c2c050426d9d60 https://gyazo.com/3f323b7beaa6a3f441835f111bcb9d16 – ThorNiLs Mar 11 '17 at 14:43
  • try `firebase.database().ref('users/'+uid+'/balance').set(100)` –  Mar 11 '17 at 14:47
  • Also, I don't think you'll be able to call `firebase.auth().currentUser()` in the same method as `createUserWithEmailAndPassword` –  Mar 11 '17 at 14:49
  • So you think I need to sign in the user as well before i can get the UID? And what does "+uid+" indicate? – ThorNiLs Mar 11 '17 at 15:05
  • Yes, you can't access auth().currentUser() unless the user is signed in. And the +uid+ is a way to insert a variable into a string. At the moment you were writing to `users/uid` which is static but you need to write to that specific user's ID which usually looks something like this `wHnubnbDKsOsWgrLO0pMdLFW18g1`. Basically the + pastes strings together. –  Mar 11 '17 at 15:09
  • Oakay, cool I didn't know about +uid+. But this is my current code in the javascript file: https://gyazo.com/54ec4f218f6ce2cfacf0697829e5640d I am testing the database without any UID or anything, and even that isnät working... Can I control that my database is even connected to my document? – ThorNiLs Mar 11 '17 at 15:29
  • http://stackoverflow.com/questions/11351689/detect-if-firebase-connection-is-lost-regained –  Mar 11 '17 at 15:36
  • Okay, so my project is connected to the database, and I managed to change the tree a bit! But have a look at this code: https://gyazo.com/fa85b17f470b490d0f2fd4ae83b9df6a the last nice alert(user.uid) returns "undefined". WHY?! – ThorNiLs Mar 11 '17 at 16:08
  • Ahhh yes, that works. And the databse seems to be working as well! Although, do you know how I write into the database from a variable? If i put useruid as key, then the key in the database wont be the uid, it will be "useruid" https://gyazo.com/8cfd887fa5ccfcade11cab090d89a1c3 – ThorNiLs Mar 11 '17 at 16:32
  • `var database = firebase.database().ref("users/"+user.uid);` –  Mar 11 '17 at 16:34
  • Ah, clever. But, still something goes wrong...! https://gyazo.com/e132c850255bc923ffaad8453f9f3a4c – ThorNiLs Mar 11 '17 at 16:55
  • Do it inside the `.then(function(user){ alert(user.uid) })` block –  Mar 11 '17 at 16:58
  • Ahh this is really becoming something! The variable problem is still there though, because it appears like this in the db: https://gyazo.com/8d80365dbe7a7e7f0830901ff17a35da And firebase requires me to edit the key, and the key is a variable.. – ThorNiLs Mar 11 '17 at 17:14
  • Just remove the `{useruid: {}}` and just replace it with `balance: 10` from the `database.set()` method –  Mar 11 '17 at 17:16