2017-09-26 6 views
3

私はaxios呼び出しから取得したデータを使用しようとしています。私は応答で正しい情報を取得しますが、応答を返そうとすると呼び出し関数で定義されません。 response.dataを呼び出し関数に返す別の方法はありますか?Axiosから取得したデータを使用してください。

static getRequest(url) { 
require('es6-promise').polyfill(); 
axios({ 
    method: 'get', 
    url: url, 
    responseType:'json', 
    withCredentials: true 
}) 
    .then(response => { 
    console.log(response.data); 

    return response.data; 
    }) 
    .catch(error => { 
    if (error.response) { 
     // The request was made and the server responded with a status code 
     // that falls out of the range of 2xx 
     console.log('___________ERROR RESPONSE__________'); 
     console.log(error.response.data); 
     console.log(error.response.status); 
     console.log(error.response.headers); 
    } else if (error.request) { 
     // The request was made but no response was received 
     // `error.request` is an instance of XMLHttpRequest in the browser and an instance of 
     // http.ClientRequest in node.js 
     console.log('_________ERROR REQUEST_______'); 
     console.log(error.request); 
    } else { 
     // Something happened in setting up the request that triggered an 
     Error 
     console.log('Error', error.message); 
    } 
    console.log('_________ERROR CONFIG_________'); 
    console.log(error.config); 
    }); 
} 

答えて

3

また、あなたのgetRequest機能からあなたのaxiosコールを返す必要があります。

上記のコードでは、axiosの約束を返すだけです。次のコードは、getRequestが呼び出されたときにresponseの値を返します。

static getRequest(url) { 

    return axios({ 
     method: 'get', 
     url: url, 
     responseType:'json', 
     withCredentials: true 
    }).then(response => { 
     return response.data 
    }) 

    //rest of code here 
} 
+0

ありがとうございます。 – Totals

関連する問題