【发布时间】:2022-01-12 05:53:25
【问题描述】:
我正在使用 prismic 制作应用程序,生成帖子,但在 GetServerSideProps 中,我遇到类型错误:对象类型为“未知”错误。我已经尝试过打字,但它不起作用或者我做得不对,有人可以帮我解决这个问题吗a
import { GetServerSideProps } from "next";
import Head from "next/head";
import { RichText } from "prismic-dom";
import { getPrismicClient } from "../../services/prismic";
import styles from "./post.module.scss";
import { ParsedUrlQuery } from "querystring";
interface IParams extends ParsedUrlQuery {
slug: string;
}
interface PostProps {
post: {
slug: String;
title: String;
content: string;
updatedAt: String;
};
}
export default function Post({ post }: PostProps) {
return (
<>
<Head>
<title>{post.title} | Ignews</title>
</Head>
<main className={styles.container}>
<article className={styles.post}>
<h1>{post.title}</h1>
<time>{post.updatedAt}</time>
<div
className={styles.postContent}
dangerouslySetInnerHTML={{ __html: post.content }}
/>
</article>
</main>
</>
);
}
export const getServerSideProps: GetServerSideProps<PostProps> = async (
context
) => {
const { slug } = context.params as IParams;
const prismic = getPrismicClient();
const response = await prismic.getByUID("publications", String(slug), {});
const post = {
slug,
title: RichText.asText(response.data.title), //Type error: Object is of type 'unknown'.
content: RichText.asHtml(response.data.content), //Type error: Object is of type 'unknown'.
updatedAt: new Date(response.last_publication_date).toLocaleDateString(
"pt-BR",
{
day: "2-digit",
month: "long",
year: "numeric",
}
), //Type error: Object is of type 'unknown'.
};
return {
props: {
post,
},
};
};
【问题讨论】:
-
您能告诉我们您是如何尝试输入的吗?可以分享一下
getPrismicClient函数的代码吗?
标签: typescript next.js