在异步函数中返回获取的问题

时间:2020-10-20 19:27:57

标签: javascript react-native api asynchronous fetch

晚上好

我目前遇到问题,我希望能够返回所获取的内容,例如:

{"data": [{"account_id": 134519399, "account_url": "Pseudo", "ad_type": 0, "ad_url": "", "animated": false, "bandwidth": 110238, "datetime": 1603190775, "deletehash": "Mg3FROsdfPF7N", "description": null, "edited": "0", "favorite": false, "has_sound": false, "height": 368, "id": "3HzN2Ye", "in_gallery": false, "in_most_viral": false, "is_ad": false, "link": "https://i.imgur.com/lbWS8uo.jpg", "name": "unnamed.jpg", "nsfw": null, "section": null, "size": 55119, "tags": [Array], "title": null, "type": "image/jpeg", "views": 2, "vote": null, "width": 512}], "status": 200, "success": true}

但我目前正在收到此消息:

{"_U": 0, "_V": 0, "_W": null, "_X": null}

这是我的代码:

export default async function FetchImage(access_token, path_query) {
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer " + access_token);

var formdata = new FormData();
var imageRec

var requestOptions = {
    method: 'GET',
    headers: myHeaders,
    redirect: 'follow'
};
return fetch("https://api.imgur.com/3/account/me/images", requestOptions)
    .then(response => response.json())
    .catch(error => console.log('error', error))
}

然后在此类中调用我的函数:

var ress;
    export default class ImageNav extends Component {
    constructor(props) {
        super(props);
    }
 
    _getAccessToken() {
        var res = this.props.navigation.state.params.access_token;
        return res
    }
 
    render() {
        // console.log(this._getAccessToken())
        ress = FetchImage(this.props.navigation.state.params.access_token)
        console.log(ress)
        return (
            <>
                <View>
                    <Text>access_token: {this._getAccessToken()}</Text>
                </View>
                <View style={styles.buttonFav}>
                    <Button buttonStyle={{ backgroundColor: '#4D4D4D' }} title="FavNav" onPress={() => this.props.navigation.navigate('FavNav')}></Button>
                </View>
                <View style={styles.buttonFind}>
                    <Button buttonStyle={{ backgroundColor: '#4D4D4D' }} title="FindNav" onPress={() => this.props.navigation.navigate('FindNav')}></Button>
                </View>
            </>
        )
    }
}

我看过论坛,但是找不到解决方法。

如果有人可以帮助我,

谢谢!

0 个答案:

没有答案