我正在尝试从操作负载中获取数据以在 redux-saga 中使用,但它返回 undefined
这是一些代码
行动
export const GetUserInfo = (user, password) => {
return{
type: actionList.GET_USER_INFO,
data: {user, password},
}
};
传奇部分
登录传奇
function* loginSaga(action) {
console.log('Saga is working')
const getJson = yield call(requestGetUser)
const getJsonData = getJson
const getJsonUsername = String(getJsonData.username)
console.log(getJson)
console.log('getjson data ', getJsonUsername)
console.log(action.data.username) // try to get data but return undefined
const getJsonPassword = String(getJsonData.password)
if (String(action.data.username) === getJsonUsername) {
if (String(action.data.password) === getJsonPassword) {
console.log('saga login success')
yield put({type: 'LOGIN_SUCCESS'})
SaveToAsyncStorage(action.data)
}
else {
console.log('saga password fail')
}
}
else {
console.log("saga user fail")
}
}
export {loginSaga}
WatchSaga
export function* watchSaga() {
yield takeLatest(GET_USER_INFO,loginSaga);
}
主文件使用 useDispatch(username,password)
来存储这些数据,它显示在 redux 扩展名中,但返回 undefined
这是一个 gif 来显示正在发生的事情
非常感谢
答案 0 :(得分:1)
我仔细查看了您提供的存储库,有一个错误,您必须进行如下更改;
替换以下代码:
export const GetUserInfo = (user, password) => {
return{
type: actionList.GET_USER_INFO,
data: {user, password},
}
};
使用以下代码
export const GetUserInfo = (username, password) => {
return{
type: actionList.GET_USER_INFO,
data: {username, password},
}
};
原因是您试图从操作中获取用户名属性,但返回属性是用户。