2017-11-07 20 views
1

この問題を解決するためのサポートが必要です。私は反応ネイティブとjavascriptで新しいです。今、私は、APIを使ってネイティブのアプリケーションを接続しようとしています。このプロセスでは、私はaxios.getを実行してデータを取得する前に、まずトークンをaxios.postで取得する必要があります。axios getリクエスト返信要求がエラー400で失敗しました

短い話ですが、以下は両方のコードスニペットです。

... // code 
const TOKEN_URL = 'https://test.co/testing/tokens' 
const DATA_URL = 'https://test.co/testing/data/page1' 

const getToken =() => { 
    axios.post(TOKEN_URL, { 
     email: 'email', 
     password: 'password', 
     role: 'user' 
    }) 
    .then((response) => { 
    //console.log(response.data.token); 
    return response.data.token; 
    }) 
    .catch((error) => { 
     console.log(error); 
    }); 
}; 

//'export' here is for use in other code: example onPress function 
export const fetchDriver =() => { 
    const config = { 
     headers: { 
      'Bearer': getToken() 
     } 
    }; 

    axios.get(DRIVER_URL, config) 
     .then((response) => { 
      console.log(response); 
     }) 
     .catch((error) => { 
      console.log(error); 
     });    
}; 

私の予想されるコンソールログは、この

{ 
    "timestamp": 1510038433, 
    "verb": "GET", 
    "object": "student", 
    "data": { 
     "age": "12", 
     "id": "90000", 
     "name": "Test Student", 
     "emergencyName": "asd", 
     "createdAt": "2017-10-04T05:39:39+00:00" 
    } 
} 

ようなものになるだろう。しかし、私はこのアプリの開発に万博を使用していますRequest failed with status code 400

を言ってエラーを取得しておきます。エラーの

詳細は、エラーがそこから来る場合、私は、API /サーバを編集するための任意の許可を持っていないこの

- node_modules/axios/lib/core/createError.js:16:24 in createError 
- node_modules/axios/lib/core/settle.js:19:6 in settle 
- node_modules/axios/lib/adapters/xhr.js:78:13 in handleLoad 
- node_modules/event-target-shim/lib/event-target.js:172:43 in dispatchEvent 
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:540:23 in 
setReadyState 
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:381:25 in 
__didCompleteResponse 
- node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:182:12 in 
emit 
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:306:47 in 
__callFunction 
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:108:26 in 
<unknown> 
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:269:6 in 
__guard 
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:107:17 in 
callFunctionReturnFlushedQueue 

のようなものです。

スニペットで欠落している点がある場合はお手伝いをしてください。 ご協力いただきありがとうございます。

+0

よう

何かがあなたの入手トークン()ポストは、有効なトークンを返していますか? – John

+0

'console.log(response.data.token)'でチェックし、トークンを正しく返します。私はトークンを直接ハードコーディングしようとしますが、結果は同じです。 – Ling

答えて

0

あなたの要求は連鎖していません。あなたはそれを使用できるようにトークンを取得するまで待たなければなりません。この

入手トークン

const getToken =() => { 
    return axios.post(TOKEN_URL, { 
     email: 'email', 
     password: 'password', 
     role: 'user' 
    }) 
    .then((response) => { 
    //console.log(response.data.token); 
    return response.data.token; 
    }) 
    .catch((error) => { 
     console.log(error); 
    }); 
}; 

fetchDriver

export const fetchDriver =() => { 
    return getToken().then(token => { 
    const config = { 
     headers: { 
     'Bearer': token 
     } 
    }; 

    return axios.get(DRIVER_URL, config) 
     .then((response) => { 
     console.log(response); 
     }) 
     .catch((error) => { 
     console.log(error); 
     }); 
    }); 
} 
+0

'未処理の約束を拒否しました。[TypeError:未定義はオブジェクトではありません(getToken()を評価する])' – Ling

+0

'getToken'をインポートしていますか?両方の機能は同じファイルにありますか? – QoP

+0

はい。両方とも同じファイルにある – Ling

関連する問題