首页 > 解决方案 > AsyncStorage:搜索值

问题描述

我有一张我users在 asyncStorage 中调用的表,可用于保存多个users详细信息(即多个用户可以仅使用一部手机登录应用程序)。

我有

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

然后使用JSON.stringify()

现在,如何检索正确的用户信息?

我沿着这条线思考,但由于使用不知道如何实现它

异步存储

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

我也想到了这条线,但肯定做错了

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');
  };

我该怎么办?

标签: javascriptreactjsreact-native

解决方案


用户必须是数组而不是对象。例如

const users = [ 
{ email: 'test@test.com', password: '123', sex: 'male'},
{ email: 'test@test.com', password: '123', sex: 'male'}
{ email: 'test@test.com', password: '123', sex: 'male'}
]

另一个代码

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

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

推荐阅读