2016-10-12 8 views
0

responseの存在を確認しないと、次のコードフローでエラーが発生します。しかし、const responseの定義では、応答が利用可能であることを保証する必要があります。なぜフローはresponseの存在のチェックを省略することができないのですか?なぜ定義されている未定義の値がチェックされますか?

/* @flow */ 

// ... import dependencies 

export function* loadDepartments(): Generator<*, *, *> { 
    try { 
    const response = yield call(getJson, endpoints.departments); 
    if (response && typeof response.data !== 'undefined') { 
     yield put(actions.loadingDepartmentsSucceeded(response.data)); 
    } 
    } catch (errors) { 
    yield put(actions.loadingDepartmentsFailed(errors)); 
    } 
} 
+0

私は 'response.data'を途中で確認することができますが、そのプロパティが存在するという保証はありません。 – vkjb38sjhbv98h4jgvx98hah3fef

答えて

3

イベントあなたがconstを使用した場合、responseの値がどのようなあなたのcall機能の利回りに応じて、undefinedすることができます。 constで定義された変数は、実際には値undefinedを含むことができます。場合response

あなたはundefinedのプロパティ「データ」を読み取ることができないので、チェック

if (typeof response.data !== 'undefined') { 

はTypeErrorを発生させます、未定義です。

+0

ああ、感謝します、ありがとう! – vkjb38sjhbv98h4jgvx98hah3fef

関連する問題