ReactNative 异步函数 return [object 对象]

ReactNative Async function return [object Object]

嗨,我是 React Native 的新手...我正在尝试使用以下函数从 AsyncStorage 获取值。并尝试在警戒状态下取回它。但是我一直在接收值 [object Object]。在控制台日志上,它可以显示该值,但是一旦 return 它就变成了对象。我可以知道为什么吗?

下面是我的函数..

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

因为您的 _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>