对象作为 React 子对象无效(已找到:带键的对象..{})。如果您打算呈现子集合,请改用数组

Objects are not valid as a React child (found: object with keys..{}). If you meant to render a collection of children, use an array instead

我正在尝试获取登录用户关注的用户的帖子。

这是后端代码。

router.get("/posts",checkAuth,async(req,res,next)=>{
    let user;
    let loginUserId=req.user.id;
    try{
        user=await User.findById(loginUserId);
   }
   catch(err){
       console.log(err);
       const error=new HttpError("Couldnt able to find People",500);
      return next(error);
   }
//    console.log(user)

   let followingsPosts=user.followings;


    let posts= await Post.find({creator:{$in:followingsPosts}}).populate("creator");

    console.log(posts);
   
    res.json(posts);
//    res.json(numFruits[0]);
});

然后我将这个帖子数组发送到我的 actionHandler 文件

export const getPeoplePosts = () => (dispatch,getState) => {
  dispatch(setPostsLoading());
  axios
    .get(`http://localhost:5000/api/people/posts`,tokenConfig(getState))
    .then(res=>
      dispatch({
        type: GET_PEOPLE_POSTS,
        payload: res.data
      })
    )
    .catch(err =>
      dispatch(returnErrors(err.response.data, err.response.status))
    );
};

然后将此操作分派到 reducer 文件

//state of reducer file
const initialState = {
    posts: [],
    peoplePosts:[],
    loading: false
  };
  
  case GET_PEOPLE_POSTS:
          return {
            ...state,
            peoplePosts:       [...action.payload],
            loading: false
          };
  
  

然后我收到错误。

未处理的拒绝(TypeError):无法读取未定义的 属性 'data'

32 |公理 33 | .get(http://localhost:5000/api/people/posts,tokenConfig(getState)) 34 | .then(res=>

35 | dispatch({ | ^ 36 | type: GET_PEOPLE_POSTS, 37 | payload: res.data 38 | })

我搞错了,哈哈 我试图在反应中打印创建者,因为以前它是创建者的 ID,但在填充创建者之后我得到了对象。