【问题标题】:Objects are not valid as a React child . If you meant to render a collection of children, use an array instead对象作为 React child 无效。如果您打算渲染一组孩子,请改用数组
【发布时间】:2021-12-26 19:28:19
【问题描述】:

我的教师模型架构是这样的:

tchStudents: [{
  type: Schema.Types.ObjectId, 
  ref: "Student"
}]

接下来是使用mongoose-paginate进行填充

const options = {
  select:  'tchID tchFirstName tchLastName tchStudents',
  sort:     { date: -1 },
  populate: 'tchStudents',
};

try {

  const tchID = await Teacher.paginate(req.query.tchID ? {tchID: req.query.tchID} : 
  {}, {limit, page, skip, options})
                       

  return res.status(200).json({
     success: true,
     data: tchID
  })

} catch(err){
  console.log(err);
}

问题是如何在我的react.js 上渲染(声明Schema.ObjectID 并在浏览器上渲染和填充)?

<td>{enNum}</td>
<td>{enSclYear}</td>
<td>{enGrade}</td>
<td>{enSection}</td>
<td><Moment format="YYYY/MM/DD">{enDate}</Moment></td>
<td>{enStudent}</td> // It is a schema.ObjectId and populate on it 
(I want to render on it)
<td>
<Link 
   to={`/showEnroll/${enroll._id}`}
>
<Button
   tooltip="Edit Student"
   tooltipOptions={{
      position: 'left'
   }}
   className="btn blue darken-4"
>
<i className="material-icons center">create</i>
</Button>
</Link>
</td>

非常感谢……

【问题讨论】:

    标签: reactjs mongoose


    【解决方案1】:

    通过查看您的 JSX 代码,没有父元素。所有孩子都处于同一水平,这可能是您收到此错误的原因。您可以为此使用 React.Fragments。

    <React.Fragment>
        <td>Test</td>
        <Link to='/'>
            <Button>Test</Button>
        </Link>
    </React.Fragment>
    

    参考:https://reactjs.org/docs/fragments.html

    【讨论】:

    • 谢谢我解决了这个问题
    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 2021-12-05
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    相关资源
    最近更新 更多