2017-12-06 11 views
0

나는 redux와 ReactJs에있는 초심자이다. 문제가 발생하면 로그인 API를 호출하지 않으려 고 응답을 얻지 못했습니다.반응하는 js에 들어가는 것에 api 응답을 응답하십시오

import { CALL_API } from 'redux-api-middleware'; 
export function login(toSend) { 
    return function(dispatch, getState) { 
     dispatch(mopetsLogin(toSend)).then(() => 
     { 
      console.log(getState().mopets_api.token.access_token); 
      dispatch(mopetsMe(getState().mopets_api.token.access_token)); 
     }); 
    } 
} 

export function mopetsLogin(toSend) { 
    console.log('toSend'); 
    console.log(toSend); 
    return { 
     [CALL_API]: { 
      endpoint: 'http://api.mopets.com/app_dev.php/login', 
      method: 'POST', 
      /*headers: { 'Content-Type': 'application/json' },*/ 
      body: JSON.stringify(toSend), 
      types: [ 
       'INITIAL_LOGIN_MOPETS_REQUEST', 
       { 
        type: 'INITIAL_LOGIN_MOPETS_SUCCESS', 
        payload: (action, state, res) => { 
         const contentType = res.headers.get('Content-Type'); 
         if (contentType && ~contentType.indexOf('json')) { 
          // Just making sure res.json() does not raise an error 
          const contentType = res.headers.get('Content-Type'); 
          if (contentType && ~contentType.indexOf('json')) { 
           // Just making sure res.json() does not raise an error 
           return res.json().then((json) => { 
             var o = new Object(); 
             o["token"] = json; 
             return o; 
            } 
           ) 
          } 
          ; 
         } 
        } 
       }, 
       'INITIAL_LOGIN_MOPETS_FAILURE' 
      ] 
     } 
    } 
} 

main.bundle.js:42605 Uncaught (in promise) TypeError: Cannot read property 'access_token' of undefined

코드를보고이 문제를 해결하기 위해 몇 가지 정보를 제공하시기 바랍니다 수 을 보여주는 이러한 유형의 오류입니다. 당신의

getState().mopets_api.token 

이 정의되어 있기 때문에

+1

귀하의 발송 기능에는 2 개의 '다음'이 있습니다. – Thinker

+0

하나는 삭제했지만 여전히 @Thinker –

+0

'오류가 있습니다. then (loginResult => console.log (loginResult.token))'? 또한'new Object'를 사용하지 마십시오'return {token : json};' –

답변

0

이 전나무를 appens.

경로 문제 일 수 있습니다. console.log를 사용하여 개체가 올바르게 채워 졌는지 확인하십시오.

+0

console.log (getState(). mopets_api를 확인했으나 null 객체를 표시 함) @ user1234087 –