Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined
Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined
当我想获取它时,我已经使用 AsyncStorage 存储了数据用户,它在警报中呈现 null 并且在控制台中出现上述错误,我不知道为什么。
我不明白为什么它 returns 对用户无效,但令牌一切正常
profile.js :
import React, {Component} from 'react';
import { StyleSheet, Text, View, Image, TouchableOpacity,AsyncStorage}
from 'react-native';
import { Container, Content, Header,Right, Left, Body} from 'native-
base';
import Feather from 'react-native-vector-icons/Feather';
export default class Profile extends Component {
constructor (props){
super(props);
this.state = {
user: null
}
}
componentDidMount(){
this.fetchData();
}
fetchData = async() =>{
let username = await AsyncStorage.getItem('user')
this.setState({
user: username
})
alert(user)
console.log(user)
};
render(){return (....)}
这里是我存数据用户和token的时候。
Signin.js :
signInMethod = () => {
this.setState({
showProgress : true
});
return fetch(url, {
method: "POST",
headers: {
'Accept': "application/json",
'Content-Type': "application/json",
},
body : JSON.stringify({
email : this.state.userEmail,
password : this.state.userPassword
})
})
.then((response) => response.json())
.then((res) => {
//if there was an error
if(res.error){
//Alert.alert("رسالة","المرجو التأكد من صحة البيانات");
this.setState({
error: 'المرجو التأكد من صحة البيانات'
});
return;
}
//if there was no error then store the jwt in the localstorage
this.setState({
customerData : res.customer
});
if (this.state.customerData && res.token) {
//we store the user data to be used localy
AsyncStorage.setItem('user',JSON.stringify(res.customer));
AsyncStorage.setItem('jwt', res.token);
AsyncStorage.getItem('user');
updateLogin(res.token);
this.props.navigation.navigate('Home');
}
})
.catch((err) => {
Alert.alert("المرجو اعادة المحاولة لاحقا");
})
}
请帮忙。
你做错了。
如果您需要打印用户名,您必须这样做 console.log(username)
。
alert
也是如此。
如果你想打印状态,它不会显示任何输出,因为setState({})
函数调用是批处理的,结果只会在后续的[=14=中对你可见]
例如
render() {
console.log(this.state.username); return(...)}
希望这个答案
我解决了这个问题:
profile.js:
fetchData = async() =>{
let userData = await AsyncStorage.getItem('user', (value) => {
JSON.parse(value);
});
//console.log(userData)
this.setState({
user: userData
})
alert(this.state.user)
//console.log(this.state.user.name);
}
我现在发现的问题是,当我想显示数据用户,例如姓名、电子邮件...
<Text>{this.state.user.name}<Text>
我收到此错误:无法读取 属性 'name' of null
当我想获取它时,我已经使用 AsyncStorage 存储了数据用户,它在警报中呈现 null 并且在控制台中出现上述错误,我不知道为什么。
我不明白为什么它 returns 对用户无效,但令牌一切正常
profile.js :
import React, {Component} from 'react';
import { StyleSheet, Text, View, Image, TouchableOpacity,AsyncStorage}
from 'react-native';
import { Container, Content, Header,Right, Left, Body} from 'native-
base';
import Feather from 'react-native-vector-icons/Feather';
export default class Profile extends Component {
constructor (props){
super(props);
this.state = {
user: null
}
}
componentDidMount(){
this.fetchData();
}
fetchData = async() =>{
let username = await AsyncStorage.getItem('user')
this.setState({
user: username
})
alert(user)
console.log(user)
};
render(){return (....)}
这里是我存数据用户和token的时候。
Signin.js :
signInMethod = () => {
this.setState({
showProgress : true
});
return fetch(url, {
method: "POST",
headers: {
'Accept': "application/json",
'Content-Type': "application/json",
},
body : JSON.stringify({
email : this.state.userEmail,
password : this.state.userPassword
})
})
.then((response) => response.json())
.then((res) => {
//if there was an error
if(res.error){
//Alert.alert("رسالة","المرجو التأكد من صحة البيانات");
this.setState({
error: 'المرجو التأكد من صحة البيانات'
});
return;
}
//if there was no error then store the jwt in the localstorage
this.setState({
customerData : res.customer
});
if (this.state.customerData && res.token) {
//we store the user data to be used localy
AsyncStorage.setItem('user',JSON.stringify(res.customer));
AsyncStorage.setItem('jwt', res.token);
AsyncStorage.getItem('user');
updateLogin(res.token);
this.props.navigation.navigate('Home');
}
})
.catch((err) => {
Alert.alert("المرجو اعادة المحاولة لاحقا");
})
}
请帮忙。
你做错了。
如果您需要打印用户名,您必须这样做 console.log(username)
。
alert
也是如此。
如果你想打印状态,它不会显示任何输出,因为setState({})
函数调用是批处理的,结果只会在后续的[=14=中对你可见]
例如
render() {
console.log(this.state.username); return(...)}
希望这个答案
我解决了这个问题:
profile.js:
fetchData = async() =>{
let userData = await AsyncStorage.getItem('user', (value) => {
JSON.parse(value);
});
//console.log(userData)
this.setState({
user: userData
})
alert(this.state.user)
//console.log(this.state.user.name);
}
我现在发现的问题是,当我想显示数据用户,例如姓名、电子邮件...
<Text>{this.state.user.name}<Text>
我收到此错误:无法读取 属性 'name' of null