javascript - 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');
};
我该怎么办?
解决方案
用户必须是数组而不是对象。例如
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')
}
推荐阅读
- python - 为什么使用 OneHotEncoding 可以更好地执行 NN?
- reactjs - 使用 API 身份验证反应私有路由验证
- python - Sympy 向量加法返回 sympy.core.add.Add 而不是 sympy.vector.vector.VectorAdd
- processing - 如何使事件每 8 帧和最后两帧(处理)发生一次?
- r - 将多个值与 R Dataframe 中的多个值进行比较
- java - 即使 GlobalHandler 类在其他包中,@ControllerAdvice 也无法正常工作-Spring MVC 异常处理
- swift - 获得使用 ScriptingBridge 的权限
- angularjs - 当我单击按钮时,角度不起作用..... Spring mvc
- android - Appium,当我尝试从 Native 切换到 Webview 时出现错误
- python - 无法从字符串中删除某些符号?