第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在異步函數(shù)中返回提取的問題 | 第2部分

在異步函數(shù)中返回提取的問題 | 第2部分

江戶川亂折騰 2023-05-25 17:28:21
我目前遇到了一個(gè)問題,我希望能夠返回提取接收到的內(nèi)容,例如:{"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}但我目前收到這個(gè):{"_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);}}
查看完整描述

3 回答

?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

好的,我找到了解決方案,我必須對(duì)setState收到的內(nèi)容進(jìn)行處理。


async componentDidMount() {

        try {

            let res = await FetchImage(this.props.navigation.state.params.access_token);

            this.setState({ test: res })

        } catch (error) {

            console.error(error);

        }

    }

我把它放在componentDidMount()函數(shù)中,這樣就setState不會(huì)循環(huán)。



查看完整回答
反對(duì) 回復(fù) 2023-05-25
?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

response.json()也是Promise。所以你應(yīng)該附加await在它前面。


export default async function FetchImage(access_token) {

var myHeaders = new Headers();

myHeaders.append("Authorization", "Bearer " + access_token);

myHeaders.append("Content-Type", "application/json");


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 await response.json()

}

catch (err) {

    console.log('fetch failed', err);

}

}


查看完整回答
反對(duì) 回復(fù) 2023-05-25
?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

我剛剛看到,我的解決方案之后產(chǎn)生了同樣的問題


?async _getData() {

? ? try {

? ? ? ? this.state.test = await FetchImage(this.props.navigation.state.params.access_token);

? ? ? ? console.log(this.state.test.data[0]["link"])

? ? ? ? return (this.state.test)


? ? } catch (error) {

? ? ? ? console.error(error);

? ? }

? ? // console.log(res)


}

我在這里稱呼他:


render() {

? ? ? ? try {

? ? ? ? ? ? console.log(this._getData())

? ? ? ? ? ??

? ? ? ? } catch (error) {

? ? ? ? ? ? console.error(error);

? ? ? ? }

? ? ? ? console.log("ici = " + this.props.test)

? ? ? ? return (

? ? ? ? ? ? <>

? ? ? ? ? ? ? ? <View>

? ? ? ? ? ? ? ? ? ? <Text>access_token:</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>

? ? ? ? ? ? </>

? ? ? ? )

? ? }

}

但是,當(dāng)我嘗試使用存儲(chǔ)在我的“this.state.test”中的內(nèi)容時(shí),它是“未定義的”。

console.log(this._getData()) 顯示這個(gè){"_U": 0, "_V": 0, "_W": null, "_X": null}

我很絕望 :'(


查看完整回答
反對(duì) 回復(fù) 2023-05-25
  • 3 回答
  • 0 關(guān)注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)