【发布时间】:2023-04-04 07:15:01
【问题描述】:
我正在尝试使用 mongoose 和 SSP 从 MongoDB 获取对象数组。一个问题是必须将所有 ObjectId 转换为字符串。目前我正在这样做:
export async function getServerSideProps({ query }) {
try {
const { user } = query
await connectDB()
const currentUser = await User.findOne({ user }).lean(),
{ _id } = await currentUser,
userProperties = await Property.find({ ownerId: _id }).lean()
currentUser._id = currentUser._id.toString()
userProperties.forEach(props => {
props._id = props._id.toString()
props.ownerId = props.ownerId.toString()
props.subarray.forEach(props => {
props._id = props._id.toString()
})
})
if (!currentUser) {
return {
notFound: true
}
}
return {
props: {
currentUser,
userProperties
}
}
} catch (err) {
console.log(err)
return {
redirect: {
destination: '/',
statusCode: 307
}
}
}
}
这会产生:Error: If(...): Nothing was returned from render. 我可以获取没有属性的用户,没有问题,即使没有返回任何内容,我也可以控制台记录附加的属性。这里发生了什么?
【问题讨论】:
-
"
Nothing was returned from render" - 你的 React 组件渲染是什么样的?
标签: javascript mongodb mongoose next.js server-side-rendering