我试图过滤Likes userId等于params.userId的帖子,现在我得到一个空数组。理想情况下,它应该得到params.userId喜欢的所有帖子。
选择器
export const getUserPosts = (params) => {
return createSelector(
postSelector,
(state) => {
const findIndex = state.posts.findIndex((x) => x.userId == params.userId);
console.log(findIndex)
// const likedPosts = state.posts.reduce((acc, cv) => {
// const newPosts = {
// Likes: cv.Likes.filter((item) => item.userId == params.userId)
// }
// const newerPosts = newPosts.Likes.length !== 0 ? newPosts.Likes : []
// console.log(newerPosts)
// return { acc, newerPosts }
// }, [])
// console.log(likedPosts)
return state.posts.filter((item) => item.Likes.userId == params.userId)
}
)
}
{
"id": 7,
"title": "another testf",
"postContent": "fsfsfsfsfssfsf",
"likedByMe": false,
"likeCounts": 1,
"userId": 81,
"createdAt": "2020-01-22T02:59:17.763Z",
"updatedAt": "2020-01-23T02:49:40.801Z",
"author": {
"username": "barnowl2",
"gravatar": "https://i.pravatar.cc/150?img=11",
"bio": null
},
"Likes": [
{
"id": 11,
"userId": 81,
"resourceId": 7,
"createdAt": "2020-01-23T02:32:52.761Z",
"updatedAt": "2020-01-23T02:32:52.761Z"
}
],
"Comments": []
}