react-book
react-book copied to clipboard
TypeError: can't read 'id' of undefind
有人没有登录就发了帖子,导致post的author为null;
// ./src/redux/module/posts.js
const convertPostsToPlain = posts => {
let postsById = {}
let postIds = []
let authorsById = {}
posts.forEach(item => {
// Solution for dev
item = item.author ? item : { ...item, author:{id: 123, username: 'none'}}
postsById[item.id] = { ...item, author: item.author.id }
postIds.push(item.id)
if (!authorsById[item.author.id]) {
authorsById[item.author.id] = item.author
}
})
return {
posts: postsById,
postIds: postIds,
authors: authorsById
}
}
solved