redux-sagas 生成器 yield 在回调中失败
redux-sagas generator yield failing inside callback
我正在尝试使用 sagas 中间件通过 firebase 设置 redux 注册和身份验证流程。但是我得到:
Module build failed: SyntaxError: Unexpected token (16:3)
14 | registerUser(action.creds).then(response => {
15 | if(response.payload.errorCode)
> 16 | yield put({type: 'REGISTRATION_FAILED', message: {msg_type: 'error', msg_body: response.payload.errorMessage}})
| ^
17 | else
18 | yield put({type: 'REGISTRATION_SUCCESSFUL', user: response})
19 | });
有点通用,但是移动那行代码似乎 yield 特别不喜欢在回调中。但是我没有看到任何地方特别禁止这样做。有没有更好的方法来做到这一点?这是 saga 生成器。
export function* registerUserSaga(action){
console.log('registerUser called')
console.log(action)
registerUser(action.creds).then(response => {
if(response.payload.errorCode)
yield put({type: 'REGISTRATION_FAILED', message: {msg_type: 'error', msg_body: response.payload.errorMessage}})
else
yield put({type: 'REGISTRATION_SUCCESSFUL', user: response})
});
}
这里是 firebase 注册函数:
export default FireBaseTools = {
registerUser: (creds) => {
return firebaseAuth.createUserWithEmailAndPassword(creds.email, creds.password).then(user => {
return user;
}).catch(error => {
return {
errorCode: error.code,
errorMessage: error.message
}
});
}
}
编辑:最初我认为这是由 if/else 引起的,但回调似乎才是真正的罪魁祸首
您应该使用 registerUser 函数中的 Promise;
类似的东西:
registerUser: creds => {
return firebaseAuth.createUserWithEmailAndPassword(creds.email, creds.password)
.then(user => ({user}) )
.catch(error => ({error}))
}
export function* registerUserSaga(action){
const {error, user} = yield call(registerUser,action.creds);
error
? yield put({type: 'REGISTRATION_FAILED',
message: {msg_type: 'error', msg_body: error.message}})
: yield put({type: 'REGISTRATION_SUCCESSFUL', user});
}
我正在尝试使用 sagas 中间件通过 firebase 设置 redux 注册和身份验证流程。但是我得到:
Module build failed: SyntaxError: Unexpected token (16:3)
14 | registerUser(action.creds).then(response => {
15 | if(response.payload.errorCode)
> 16 | yield put({type: 'REGISTRATION_FAILED', message: {msg_type: 'error', msg_body: response.payload.errorMessage}})
| ^
17 | else
18 | yield put({type: 'REGISTRATION_SUCCESSFUL', user: response})
19 | });
有点通用,但是移动那行代码似乎 yield 特别不喜欢在回调中。但是我没有看到任何地方特别禁止这样做。有没有更好的方法来做到这一点?这是 saga 生成器。
export function* registerUserSaga(action){
console.log('registerUser called')
console.log(action)
registerUser(action.creds).then(response => {
if(response.payload.errorCode)
yield put({type: 'REGISTRATION_FAILED', message: {msg_type: 'error', msg_body: response.payload.errorMessage}})
else
yield put({type: 'REGISTRATION_SUCCESSFUL', user: response})
});
}
这里是 firebase 注册函数:
export default FireBaseTools = {
registerUser: (creds) => {
return firebaseAuth.createUserWithEmailAndPassword(creds.email, creds.password).then(user => {
return user;
}).catch(error => {
return {
errorCode: error.code,
errorMessage: error.message
}
});
}
}
编辑:最初我认为这是由 if/else 引起的,但回调似乎才是真正的罪魁祸首
您应该使用 registerUser 函数中的 Promise;
类似的东西:
registerUser: creds => {
return firebaseAuth.createUserWithEmailAndPassword(creds.email, creds.password)
.then(user => ({user}) )
.catch(error => ({error}))
}
export function* registerUserSaga(action){
const {error, user} = yield call(registerUser,action.creds);
error
? yield put({type: 'REGISTRATION_FAILED',
message: {msg_type: 'error', msg_body: error.message}})
: yield put({type: 'REGISTRATION_SUCCESSFUL', user});
}