由于此API请求,我在这里最后几分钟一直在桌子上敲头......
我有以下代码:
佐贺:
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请求:
// 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
API请求确实返回一个有效的对象,但是Saga调用的返回始终是未定义的。任何人都可以引导我到我错的地方吗?
提前致谢!
最诚挚的问候,
布鲁诺
答案 0 :(得分:6)
你忘了return
来自你职能部门的承诺。做到这一点
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());
}
};