Redux-Observable : find() 没有 return 任何东西
Redux-Observable : find() doesn't return anything
我对 RxJs 还很陌生,到目前为止只使用过 Redux (+Thunk),所以我想了解一切是如何协同工作的。我目前正在尝试使用从登录表单提交的数据从源(json 文件)获取用户。
authEpic.js
import users from '../api/users.json';
const source = of(users);
export const authEpic = (action$) => action$.pipe(
ofType("AUTH_LOGIN_REQUEST"),
switchMap(({ authData }) => source.pipe(
find(user => authData.email === user.email && authData.password === user.password),
map(user => ({ type: "RESULTS", results: user }))
))
);
但是即使它说返回的值作为属性,也没有返回任何内容。我想了解如何找到用户并将其传递给另一个操作,因为尽管四处寻找,但我现在几乎一无所知。
非常感谢您的帮助!
问题是 of(users)
发出单个值:无论 users
是什么,大概是一个数组。所以在你的 find()
中,参数 user
实际上是完整的 users
,而不是个人用户,因此 users.email === undefined
。 find()
运算符的行为类似于 Array 的 find()
,因为它总是 return(发出)单个值,因此如果没有找到任何值,则该值是 returned( emitted) 的字面意思是值 undefined
。这就是这里发生的事情。您正在发出一个动作 { type: 'RESULTS', results: undefined }
如果 users
确实是一个数组,您可能想使用 from(users)
以便 Observable 一个一个地发出每个用户。然后它会像我想象的那样工作。这是一个演示:https://stackblitz.com/edit/redux-observable-playground-rararm?file=index.js
如果这最终成为生产代码,您需要处理未找到用户匹配的情况。
顺便说一句,我想这不是生产代码(所有密码都以明文形式作为本地 json 文件)但是对于任何阅读本文的人来说,注意这一点可能仍然有帮助,在这个特殊情况下,使用数组而不是 Observable 可能会更清楚:
export const authEpic = (action$) =>
action$.pipe(
ofType('AUTH_LOGIN_REQUEST'),
// Note this is now `map` instead of `switchMap`
map(({ authData }) => {
const user = users.find(
(user) =>
authData.email === user.email && authData.password === user.password
);
// Some how handle no match (e.g. wrong email or password)
if (!user) {
return { type: 'NO_RESULTS' };
}
return { type: 'RESULTS', results: user };
})
);
我对 RxJs 还很陌生,到目前为止只使用过 Redux (+Thunk),所以我想了解一切是如何协同工作的。我目前正在尝试使用从登录表单提交的数据从源(json 文件)获取用户。
authEpic.js
import users from '../api/users.json';
const source = of(users);
export const authEpic = (action$) => action$.pipe(
ofType("AUTH_LOGIN_REQUEST"),
switchMap(({ authData }) => source.pipe(
find(user => authData.email === user.email && authData.password === user.password),
map(user => ({ type: "RESULTS", results: user }))
))
);
但是即使它说返回的值作为属性,也没有返回任何内容。我想了解如何找到用户并将其传递给另一个操作,因为尽管四处寻找,但我现在几乎一无所知。
非常感谢您的帮助!
问题是 of(users)
发出单个值:无论 users
是什么,大概是一个数组。所以在你的 find()
中,参数 user
实际上是完整的 users
,而不是个人用户,因此 users.email === undefined
。 find()
运算符的行为类似于 Array 的 find()
,因为它总是 return(发出)单个值,因此如果没有找到任何值,则该值是 returned( emitted) 的字面意思是值 undefined
。这就是这里发生的事情。您正在发出一个动作 { type: 'RESULTS', results: undefined }
如果 users
确实是一个数组,您可能想使用 from(users)
以便 Observable 一个一个地发出每个用户。然后它会像我想象的那样工作。这是一个演示:https://stackblitz.com/edit/redux-observable-playground-rararm?file=index.js
如果这最终成为生产代码,您需要处理未找到用户匹配的情况。
顺便说一句,我想这不是生产代码(所有密码都以明文形式作为本地 json 文件)但是对于任何阅读本文的人来说,注意这一点可能仍然有帮助,在这个特殊情况下,使用数组而不是 Observable 可能会更清楚:
export const authEpic = (action$) =>
action$.pipe(
ofType('AUTH_LOGIN_REQUEST'),
// Note this is now `map` instead of `switchMap`
map(({ authData }) => {
const user = users.find(
(user) =>
authData.email === user.email && authData.password === user.password
);
// Some how handle no match (e.g. wrong email or password)
if (!user) {
return { type: 'NO_RESULTS' };
}
return { type: 'RESULTS', results: user };
})
);