我目前正在尝试显示用户使用AsyncStorage登录的次数的完整日志。
这是因为我想在用户第一次登录时显示弹出模式。
有没有办法实现这个目标?
以前,我已经能够在console.log中显示用户详细信息,该文件以字符串格式显示电子邮件和密码。
但是,我不确定是从那里去的。 for循环是适当的解决方案,还是已经实现了这个方法?
登录屏幕 -
_loginUser = async () => {
const { password, email } = this.state;
if(email == '' || password == '') {
alert('Please enter all fields');
}
else {
//save data using AsyncStorage
let loginArray = {
email: email,
password: password
}
//key of the item to set
AsyncStorage.setItem('loginArray',
JSON.stringify(loginArray));
console.log(loginArray);
// const logsInfo = await AsyncStorage.getAllKeys('loginArray');
// console.log(this.setState({ logs: logsInfo }));
this.setState({
loading: true
});
this._signInAsync();
}
}
_signInAsync = async () => {
setTimeout(() => {
this.setState({
loading: false,
});
this.props.navigation.navigate("App");
}, 2000);
};
主屏幕 -
fetchAllItems = async () => {
try {
const keys = await AsyncStorage.getAllKeys()
const items = await AsyncStorage.multiGet(keys)
this.setState({ userCredentials: items })
Reactotron.log(this.state.userCredentials);
} catch (error) {
console.log(error, "problemo")
}
}
我想您想知道用户登录您应用的次数?为此你可以在AsyncStorage
保持一个柜台。
在LOGIN SCREEN - 中,
...
// Count of last logged in
let count = 0
try {
const value = await AsyncStorage.getItem("COUNTER");
if (value !== null) {
// We have counter!!
count = parseInt(value) + 1
}
} catch (error) {
// Error retrieving data, ie. count = 0
}
let loginArray = {
email: email,
password: password,
counter: count
};
//key of the item to set
AsyncStorage.setItem("loginArray", JSON.stringify(loginArray));
AsyncStorage.setItem("COUNTER", JSON.stringify(count));
console.log(loginArray);
...
在HOMESCREEN - ,
您将获得AsyncStorage
的总数。
...
//Here you will get the cont
const items = await AsyncStorage.getItem("loginArray")
this.setState({ userCredentials: items })
...
items
包含'email','password'和'counter'。