ReactNative异步函数返回[object Object]

时间:2020-07-14 15:59:51

标签: react-native object asyncstorage

Hai我在本机响应方面很新...我正在尝试使用以下功能从AsyncStorage中获取价值。并尝试在警报时检索它。但是我一直在接受价值[object Object]。在控制台日志上,它可以显示值,但是一旦返回,它就成为对象。我可以知道为什么吗?

下面是我的功能。

    _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);
          }

        
          
    }

以下是我的提醒...

<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>

1 个答案:

答案 0 :(得分:0)

因为您的_DeviceDetails函数是异步函数。您必须在回复后显示警报。我对您的代码进行了一些更改。您可以在下面看到并尝试。

 _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);
      }

请尝试这种方式。

}

<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>