react-book icon indicating copy to clipboard operation
react-book copied to clipboard

TypeError: can't read 'id' of undefind

Open Wanlin-Lu opened this issue 6 years ago • 1 comments

有人没有登录就发了帖子,导致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
	}
}

Wanlin-Lu avatar Feb 26 '20 03:02 Wanlin-Lu

solved

Wanlin-Lu avatar Mar 02 '20 13:03 Wanlin-Lu