0

I'm using redux saga as middleware and call my request in this layer. When enable react-native debugger request works prefect, but without the debugger it seems request is not async anymore.

export function* userLoginSaga(action) {
    yield put(actions.userLoginStart());
    const response = yield GET(`${action.server}`, {
        'Content-Type': 'application/json'
    }, {
        username: action.username,
        password: action.password
    });
    if (response === 503) {
        yield put(actions.serverErrorSuccess(response))
    } else if (response.status === 200) {
        yield put(actions.userLoginSuccess(response.data));
    }
}

without react debugger, i get "undefined is not an object (response.status) ". It's not waiting for response to get the result. Please note that everything is working fine with debugger.

Martin Kadlec
  • 4,702
  • 2
  • 20
  • 33
ali mottaghian
  • 310
  • 1
  • 4
  • 11
  • Is `GET` a native react-native function or is your own wrapper? If so can you provide the definition of it? – Martin Kadlec Nov 14 '19 at 17:44
  • It's my own wrapper. Inside of GET is axios.get with promise handler. – ali mottaghian Nov 15 '19 at 03:56
  • export const GET = async (url,headers,auth) =>{ const call = await axios.get(url,{headers:headers,data:{},auth:auth}) .then(response => { return response }) .catch(error=>{ if(error.message.includes("Network Error")){ return 503 } else if(error.response && error.response.status >= 500){ return 503 } else{ return error.response } }) return call } – ali mottaghian Nov 15 '19 at 03:57
  • I'd try debugging the GET method, e.g. add some logging into the `then` callback to check if you are receiving right data at the righ time. Another log after the `yield GET...` to compare with the first one and to make sure the code execution is really getting there before it should. – Martin Kadlec Nov 15 '19 at 09:46
  • Thanks Martin, when you said what's inside the GET, it made me to check it out and i found the issue. The GET is async await and looks like you can't yield an async await function. i removed the async/await and now it's working fine. – ali mottaghian Nov 18 '19 at 04:40

0 Answers0