React Native+Redux-saga出现ReadableNativeMap无法转String错误怎么解决
问题解决方案
错误原因
- 传参格式不匹配:你定义的
addUserPassMailChild需要接收两个独立的字符串参数email、password,但你用call调用时传了单个对象{email:email, password:password},导致createUserWithEmailAndPassword拿到的第一个入参是JS对象而非字符串,原生层解析时触发ReadableNativeMap转String的类型错误。 - 异步函数未返回Promise:
addUserPassMailChild没有将auth操作返回的Promise向外抛出,redux-saga的calleffect无法正确等待异步执行结果,也不能捕获异步流程中抛出的错误。
修改代码
第一步:调整addUserPassMailChild逻辑,返回Promise并透传结果/错误
function addUserPassMailChild(email, password) { // 新增return,将异步操作的Promise返回给saga return auth() .createUserWithEmailAndPassword(email, password) .then(() => { console.log('User account created & signed in!'); // 回传需要的业务数据,供后续saga逻辑使用 return { email } }) .catch(error => { if (error.code === 'auth/email-already-in-use') { console.log('That email address is already in use!'); } if (error.code === 'auth/invalid-email') { console.log('That email address is invalid!'); } console.error(error); // 抛出错误,让上层saga的catch可以捕获 throw error }); }
第二步:修改saga中call的传参方式
export function* addUserPassMail(action) { const email = action.payload.email; const password = action.payload.password; console.log(email, password) try { // 按顺序传递两个独立参数,不要包成对象 const emailLogin = yield call(addUserPassMailChild, email, password) if (emailLogin) { yield put(AddUserSuccess(emailLogin)) yield put({ type: GET_USER_TOKEN_LISTENER, }) } } catch (error) { console.log(error) yield put(AddUserField(error)) } }
修改完成后重新运行项目即可解决该报错。
内容的提问来源于stack exchange,提问作者Zenixo
相关产品推荐
相关产品推荐

