【发布时间】:2021-02-03 17:26:03
【问题描述】:
我目前遇到一个问题,我希望能够返回 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) {
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer " + access_token);
var formdata = new FormData();
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
try {
const response = await fetch("https://api.imgur.com/3/account/me/images", requestOptions)
// console.log(await response.text());
return response.json
}
catch (err) {
console.log('fetch failed', err);
}
}
我在这个类中调用我的函数:
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>
</>
)
}
}
我查看了这些解决方案:
How do I return the response from an asynchronous call?
async/await implicitly returns promise?
但我还是不能解决我的问题,
如果有人可以帮助我
谢谢!
【问题讨论】:
标签: javascript react-native api fetch