Reason: object
(“[object Date]”) cannot be serialized as JSON. Please only return JSON serializable data types
解决方案:
使用superjson
yarn add superjson babel-plugin-superjson-next
修改.babelrc.js文件,使用superjson插件
const superjson = require('babel-plugin-superjson-next');
module.exports = {
presets: [
['next/babel']
],
plugins: [superjson]
};
最后在报错的地方使用
export const getArticleById = async (id: number) => {
const article = await prisma.article.findUnique({
where: {
id: Number(id),
},
})
return superjson.serialize(article).json
}