1

I have created a simple login app using react native that let's users signup, login, and logout. my signup function takes a username that is then used in the createUser callback to generate a db entry with the uid as the key, and the username entered as a value. The answer in this post is the structure I followed - How do you include a username when storing email and password using Firebase (BaaS) in an Android app?

After the user is logged in, I'd like to get the username and display it but I'm having trouble figuring this out.

This is the code I currently have to attempt and do it:

var ref = new Firebase("https://myreactapp.firebaseio.com");

module.exports = React.createClass({
  getInitialState: function() {
    var authData = ref.getAuth();
    var user = ref.child("users/" + authData.uid + "/username");
    return {
      username: user
    };
  },

This is how the code looks when I signup and the structure of my db.

var self = this;
    let ref = new Firebase("https://myreactapp.firebaseio.com");
    ref.createUser({
      email    : this.state.email,
      password : this.state.password
    }, function(error, authData) {
      if (error) {
        return this.setState({errorMessage: 'Error creating user'});
      } else {
        ref.child("users").child(authData.uid).set({
          username: self.state.username
         });
        console.log("Successfully created user account with uid:", authData.uid);
        ToastAndroid.show('Account Created', ToastAndroid.SHORT)
        return self.props.navigator.pop();

      }
    });

----------------Not actual code-------------------------------------------------
DB

+users
--<uid>
  -username -> value
--<uid>
  -username -> value

I try to login and get an error of maximum call stack exceeded, but I have a feeling I'm going at this the wrong way. I've looked online, but everything I found was for retrieving data that is either being added, changed or deleted. All I want is to get this username once.

Community
  • 1
  • 1
Elad Karni
  • 145
  • 1
  • 4
  • 17
  • have you already stored the user data inside `/user/userId/`? – adolfosrs Jul 30 '16 at 14:05
  • I'll edit my question to include how the data looks inside the db, but to answer your questions, I do have it there. – Elad Karni Jul 30 '16 at 14:06
  • So the only problem I see here is that you are initializing your firebase app in the "legacy way". Please, take a look in [this question](http://stackoverflow.com/questions/38590303/search-elements-object-with-specific-key-value-in-firebase-database/38590740#38590740) to get more information on how you should be initializing it. – adolfosrs Jul 30 '16 at 14:09
  • `maximum call stack exceeded` means you have an infinite loop somewhere. – John Jul 30 '16 at 15:30
  • @EladKarni Stack Overflow is a bad medium to interactively debug. Can you set up a **minimal** jsfiddle/jsbin that reproduces the problem and link that into your question? – Frank van Puffelen Jul 30 '16 at 15:46
  • I apologize if my question wasn't clear enough. The error doesn't seem that important as I have a feeling the way I'm going about the issue is the problem. So my question is what's the correct way of getting data from a firebase dB. I'm using the "legacy way" because that's what I learned and it has the functionality I'm looking for. – Elad Karni Jul 31 '16 at 00:36

0 Answers0