How to obtain data from a user with firebase and react JS?

0

My question is as follows, how can I get just one name from the database since it is throwing me more than one name example of how it looks

Now I just want to get the name of the login ID and not both

I attach the code I'm using

class HomePage extends Component {
  constructor(props) {
    super(props);

    this.state = {
      users: {}
    };
  }

  componentDidMount() {
    db.onceGetUsers().then(snapshot =>
      this.setState(() => ({
         users: snapshot.val() 
        }))
    );
  }

  render() {
    const { users } = this.state;

    return (
      <div className="App">
        <h1>Home</h1>
        <p>Todos los usuarios que inician sesión tienen acceso a la Página de inicio.</p>

        { !!users && <UserList users={users} /> }
      </div>
    );
  }
}

const UserList = ({ users }) =>
  <div className="App">
    
     {Object.keys(users).map(key =>
      <div key={key}>{users[key].username}</div>
      
     )}


  </div>

const authCondition = (authUser) => !!authUser;

export default withAuthorization(authCondition)(HomePage);

same as an image of the reference of the db

& in advance thank you very much for the help.

    
asked by Francisco Navarrete 26.07.2018 в 15:35
source

1 answer

0

I assume that 'users' is the node where you store a list of users in that case you would have a list similar to this

users
 - XXXXXXX1
    - nombre = 'TESTNOMBRE1'
    - password = '********1'
 - XXXXXXX2
    - nombre = 'TESTNOMBRE2'
    - password = '********2'

When you get the 'users' con db.ref('users').once('value') node, what you are doing is getting the entire list of users.

To obtain a specific user, what you must do is to refer to the child of users, identifying it by its ID XXXXXXX1

If I want to get the second user from my list, I should check it in this way

db.ref('users').child('XXXXXXX2').once('value')

You should also take into account that if you get the whole list of users with db.ref('users').once('value') you are downloading the entire list of users, if there are 100 thousand users within users, all those users will be downloaded to your application and that is not what what you want to do, or should not.

    
answered by 26.07.2018 в 19:08