最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sagas and fetch promises - Stack Overflow

programmeradmin0浏览0评论

I've been banging my head on the desk for the last few minutes here due to this API request...

I have the following code:

Saga:

export function * registerFlow () {
  while (true) {
    const request = yield take(authTypes.SIGNUP_REQUEST)
    console.log('authSaga request', request)
    let response = yield call(authApi.register, request.payload)
    console.log('authSaga response', response)
    if (response.error) {
      return yield put({ type: authTypes.SIGNUP_FAILURE, response })
    }

    yield put({ type: authTypes.SIGNUP_SUCCESS, response })
  }
}

API request:

// Inject fetch polyfill if fetch is unsuported
if (!window.fetch) { const fetch = require('whatwg-fetch') }

const authApi = {
  register (userData) {
    fetch(`http://localhost/api/auth/local/register`, {
      method  : 'POST',
      headers : {
        'Accept'        : 'application/json',
        'Content-Type'  : 'application/json'
      },
      body    : JSON.stringify({
        name      : userData.name,
        email     : userData.email,
        password  : userData.password
      })
    })
    .then(statusHelper)
    .then(response => response.json())
    .catch(error => error)
    .then(data => data)
  }
}

function statusHelper (response) {
  if (response.status >= 200 && response.status < 300) {
    return Promise.resolve(response)
  } else {
    return Promise.reject(new Error(response.statusText))
  }
}

export default authApi

The API request does return a valid object however the return from the Saga call always is undefined. Can anyone guide me to where I am wrong?

Thanks in advance!

Best Regards,

Bruno

I've been banging my head on the desk for the last few minutes here due to this API request...

I have the following code:

Saga:

export function * registerFlow () {
  while (true) {
    const request = yield take(authTypes.SIGNUP_REQUEST)
    console.log('authSaga request', request)
    let response = yield call(authApi.register, request.payload)
    console.log('authSaga response', response)
    if (response.error) {
      return yield put({ type: authTypes.SIGNUP_FAILURE, response })
    }

    yield put({ type: authTypes.SIGNUP_SUCCESS, response })
  }
}

API request:

// Inject fetch polyfill if fetch is unsuported
if (!window.fetch) { const fetch = require('whatwg-fetch') }

const authApi = {
  register (userData) {
    fetch(`http://localhost/api/auth/local/register`, {
      method  : 'POST',
      headers : {
        'Accept'        : 'application/json',
        'Content-Type'  : 'application/json'
      },
      body    : JSON.stringify({
        name      : userData.name,
        email     : userData.email,
        password  : userData.password
      })
    })
    .then(statusHelper)
    .then(response => response.json())
    .catch(error => error)
    .then(data => data)
  }
}

function statusHelper (response) {
  if (response.status >= 200 && response.status < 300) {
    return Promise.resolve(response)
  } else {
    return Promise.reject(new Error(response.statusText))
  }
}

export default authApi

The API request does return a valid object however the return from the Saga call always is undefined. Can anyone guide me to where I am wrong?

Thanks in advance!

Best Regards,

Bruno

Share Improve this question asked Sep 30, 2016 at 1:17 Bruno BarbosaBruno Barbosa 981 silver badge7 bronze badges 5
  • What good are .catch(error => error).then(data => data)? You really should omit them (especially the catch that transforms every rejection into a fulfillment) – Bergi Commented Sep 30, 2016 at 1:34
  • Where exactly do you get undefined? – Bergi Commented Sep 30, 2016 at 1:35
  • I am getting undefined inside the Saga, more specifically, the console.log('authSaga', response) – Bruno Barbosa Commented Sep 30, 2016 at 1:38
  • Even if I omit it the problem persists. – Bruno Barbosa Commented Sep 30, 2016 at 1:38
  • I was looking for a sample of web-api used with saga. Thanks for sharing. – Adrian Moisa Commented Mar 20, 2017 at 22:20
Add a ment  | 

1 Answer 1

Reset to default 6

You forgot to return the promises from your function. Make it

const authApi = {
  register (userData) {
    return fetch(`http://localhost/api/auth/local/register`, {
//  ^^^^^^
      method  : 'POST',
      headers : {
        'Accept'        : 'application/json',
        'Content-Type'  : 'application/json'
      },
      body    : JSON.stringify({
        name      : userData.name,
        email     : userData.email,
        password  : userData.password
      })
    })
    .then(statusHelper)
    .then(response => response.json());
  }
};
发布评论

评论列表(0)

  1. 暂无评论