Reputation: 87
I'm trying to fetch a count that is based on the post.id
that is equal to the action.id
console.log(action.data.find((post) => post.id === action.id).Likes.length)
However im getting this
Unhandled Rejection (TypeError): Cannot read property 'Likes' of undefined
but when i change it something like
action.data.find((post) => post.id === 5).Likes.length) // 5 is an id of an existing post.
it works as expected, however it needs to be dynamic.
here is the reducer
const initialState = {
post: [],
postError: null,
posts:[],
isEditing:false,
isEditingId:null,
likes:[],
someLike:[],
postId:null
}
export default (state = initialState, action) => {
switch (action.type) {
case GET_POSTS:
console.log(action.data)
console.log(action.data.find((post) => post.id === action.id).Likes.length) // fetchs likes count according to post.id but needs to be dynamic
return {
...state,
posts: action.data, // maps posts fine,
likes: action.data.find((post) => post.id === action.id).Likes.length
// needs to be dynamic so i can multiple post.ids
}
Actions.js
export const GetPosts = () => {
return (dispatch, getState) => {
return Axios.get('/api/posts/myPosts')
.then( (res) => {
const data = res.data
const id = data.map( (post) => post.id) // gets posts id [5,3]
dispatch({type: GET_POSTS, data, id})
})
}
}
Posts.js
import React, { Component } from 'react';
import PostList from './PostList';
import {connect} from 'react-redux';
import { withRouter, Redirect} from 'react-router-dom';
import {GetPosts} from '../actions/';
const Styles = {
myPaper:{
margin: '20px 0px',
padding:'20px'
}
,
wrapper:{
padding:'0px 60px'
}
}
class Posts extends Component {
state = {
posts: [],
loading: true,
isEditing: false,
}
async componentWillMount(){
await this.props.GetPosts();
const thesePosts = await this.props.myPosts
const myPosts2 = await thesePosts
this.setState({
posts: myPosts2,
loading:false
})
console.log(this.state.posts.Likes);
}
render() {
const {loading} = this.state;
const { myPosts} = this.props
if (!this.props.isAuthenticated) {
return (<Redirect to='/signIn' />);
}
if(loading){
return "loading..."
}
return (
<div className="App" style={Styles.wrapper}>
<h1> Posts </h1>
<PostList posts={this.state.posts}/>
</div>
);
}
}
const mapStateToProps = (state) => ({
isAuthenticated: state.user.isAuthenticated,
myPosts: state.post.posts
})
const mapDispatchToProps = (dispatch, state) => ({
GetPosts: () => dispatch( GetPosts())
});
export default withRouter(connect(mapStateToProps,mapDispatchToProps)(Posts));
PostList.js
render(){
const {posts} = this.props;
return (
<div>
{posts.map((post, i) => (
<Paper key={post.id} style={Styles.myPaper}>
{/* {...post} prevents us from writing all of the properties out */}
<PostItem
// or put this.state.likes in the myLikes prop
myLikes={this.props.myLikes}
myTitle={this.state.title}
editChange={this.onChange}
editForm={this.formEditing}
isEditing={this.props.isEditingId === post.id}
removePost={this.removePost}
{...post}
/>
</Paper>
))}
</div>
)
}
}
Upvotes: 1
Views: 1078
Reputation: 31565
You are trying to get a post with an id that doesn't exists.
Looking at how .find
works it will return undefined
if nothing is found.
So when you do action.data.find((post) => post.id === action.id)
, it doesn't find any post
that have the same id as action
and it return undefined
and you can't get Like
from undefined
.
I recommend check it before accessing .Like
let post = action.data.find((post) => post.id === action.id)
let likeLen = post ? post.Likes.length : somethingYouWant
I know that this answer doesn't solve exaclty you problem, but it shows what is the problem and how to solve it and I hope you can understand better what is going on and help you with your problem.
Upvotes: 1