Hai i'm quite new in react native...I'm trying to get value from AsyncStorage using below function. And try to retrieve it on alert. But i keep receiving value [object Object]. On the console log it can display the value but once return it becomes object. may i know why?
Below is my function..
_DeviceDetails = async (device) =>{
try {
let nonObj = 'hai'
const obj = await AsyncStorage.getItem(device)
let obj1 = JSON.parse(obj)
var devid = obj1.deviceId
var devnam = obj1.deviceName
var devdesc = obj1.deviceDesc
//console.log(obj + 'ads')
//return obj1.deviceName.toString;
console.log('-1-' + devid)
console.log('-2-' + devnam)
console.log('-3-' + devdesc)
return devnam
} catch (e) {
console.log('Failed to fetch the data from storage' + e);
}
}
below is my alert...
<TouchableHighlight
style={{ backgroundColor: 'blue', justifyContent: 'center' , alignContent:'center'}}
activeOpacity={0.9}
underlayColor="pink" onPress={() =>
alert(item + ' - ' + (this._DeviceDetails(item)))}>
<Text style={{
fontSize: 15,
textAlign: 'center',
marginBottom: 16,
color: 'white'
}}>{item}</Text>
</TouchableHighlight>
Because your _DeviceDetails function is async function. You must show alert after your response. I make some changes in your code. you can see below and try it.
_DeviceDetails = async (device) =>{
try {
let nonObj = 'hai'
const obj = await AsyncStorage.getItem(device)
let obj1 = JSON.parse(obj)
var devid = obj1.deviceId
var devnam = obj1.deviceName
var devdesc = obj1.deviceDesc
//console.log(obj + 'ads')
//return obj1.deviceName.toString;
console.log('-1-' + devid)
console.log('-2-' + devnam)
console.log('-3-' + devdesc)
alert(device - devnam);
return devnam
} catch (e) {
console.log('Failed to fetch the data from storage' + e);
}
Please try this way.
}
<TouchableHighlight
style={{ backgroundColor: 'blue', justifyContent: 'center' , alignContent:'center'}}
activeOpacity={0.9}
underlayColor="pink" onPress={() => this. _DeviceDetails(item)}>
<Text style={{
fontSize: 15,
textAlign: 'center',
marginBottom: 16,
color: 'white'
}}>{item}</Text>
</TouchableHighlight>