【问题标题】:ReactNative Async function return [object Object]React Native 异步函数返回 [object Object]
【发布时间】:2020-07-14 15:59:51
【问题描述】:

嘿,我在 react native 方面还是个新手……我正在尝试使用以下函数从 AsyncStorage 中获取价值。并尝试在警报中检索它。但我一直收到价值[对象对象]。在控制台日志上它可以显示该值,但一旦返回它就变成了对象。我可以知道为什么吗?

下面是我的功能..

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

【问题讨论】:

    标签: react-native object asyncstorage


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-18
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多