gbenga ogunbule
gbenga ogunbule

Reputation: 599

AsyncStorage: searching for values

I have this table I called users in asyncStorage that can be use to saved more than one users details(i.e. More than one user can login into the app using just one phone).

I have

const users = {
  email: this.state.email,
  password: this.state.password,
  sex: this.state.sex
};

Then save it using JSON.stringify()

Now, how do I retrieve the correct user info?

AM thinking along this line but don't know how to implement it due to using

AsyncStorage

SELECT email, password from users where email===this.state.email and password === this.state.password

I also think of something along this line, but sure am doing the wrong thing

getStudent = async () => {
    //function to get the value from AsyncStorage
    let users= await AsyncStorage.getItem('students');
    let allUsers= JSON.parse(users);

    // Check if entered username and password equals any username and password from the DB
    allUsers.filter(function (credentials) {
       credentials.email.includes(this.state.email);// Then, I hit the wall here
    })
    this.props.navigation.navigate('ViewProfile');
  };

How can I go about it?

Upvotes: 2

Views: 314

Answers (1)

Nijat Aliyev
Nijat Aliyev

Reputation: 894

Users must be array not object. For example

const users = [ 
{ email: '[email protected]', password: '123', sex: 'male'},
{ email: '[email protected]', password: '123', sex: 'male'}
{ email: '[email protected]', password: '123', sex: 'male'}
]

Some another code

let user = allUsers.filter(u => u.email === this.state.email)[0];

if (user) {
  this.props.navigation.navigate('/viewProfile', { data: user })
} else {
  alert ('Not found')
}

Upvotes: 1

Related Questions