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



I have stored data user with AsyncStorage when i want to get it, it renders null in alert and the above error in the console,i don't know why.
I can not see why it returns null with user but token all is well



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 (....)}



here when i stock the data user and 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("المرجو اعادة المحاولة لاحقا");
})
}



help please.





does your user key exist in asyncstorage?
– Aseem Upadhyay
Jun 29 at 10:37



user





yes, AsyncStorage.setItem('user', res.customer), the object res.customer contains other Objects, i make the same thing for the token all is good but with user no
– hanae
Jun 29 at 10:42




2 Answers
2



You are doing it wrong.



If you need to print out the username you'll have to do console.log(username).


console.log(username)



The same goes for alert.


alert



And if you wish to print out the state, it'll not show you any output because setState({}) function calls are batched, and the result will only be visible to you in the subsequent render()


setState({})


render()



For example


render() {
console.log(this.state.username); return(...)}



Hope this answers





with your solution the result gives undefined
– hanae
Jun 29 at 11:02





check updated answer
– Aseem Upadhyay
Jun 29 at 11:03





also returns null
– hanae
Jun 29 at 11:17





can you post your whole component with the minimal necessary logic for this question
– Aseem Upadhyay
Jun 29 at 11:31





I did it, check for the update
– hanae
Jun 29 at 15:27




I resolve the problem :
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);
}



the problem what i find now is when i want to show data user such as name, email...


<Text>{this.state.user.name}<Text>



I get this error : Cannot read property 'name' of null






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV