微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 pt::text

如何解决使用 pt::text

我正在尝试使用 sanity-algolia 库(参考 https://www.sanity.io/plugins/sanity-algolia)将 algolia 搜索与 sanity CMS 集成

但是当我尝试使用 pt::text 函数从 Portable Text 富文本内容获取纯文本时。 我得到 expected '}' following object body ,我真的不知道我在哪里缺少括号。

(另一个注意事项:由于我使用 sanity start 托管 sanity,我使用 nextjs(我的前端)来运行该函数,而不是使用 nextJS 中的 /api 路由)所以 sanity 有一个 webhook 到这条路由。

有关错误的详细信息:

  details: {
    description: "expected '}' following object body",end: 174,query: '* [(_id in $created || _id in $updated) && _type in $types] {\n' +
      '  _id,\n' +
      '  _type,\n' +
      '  _rev,\n' +
      '  _type == "post" => {\n' +
      '        title,\n' +
      '        "path": slug.current,\n' +
      '        "body": pt::text(body)\n' +
      '      }\n' +
      '}',start: 107,type: 'queryParseError'
  }

这是我正在运行的无服务器功能

export default async function handler(req,res) {
  if (req.headers["content-type"] !== "application/json") {
    res.status(400);
    res.json({ message: "Bad request" });
    return;
  }

  const algoliaIndex = algolia.initIndex("dev_kim_miles");

  const sanityAlgolia = indexer(
    {
      post: {
        index: algoliaIndex,projection: `{
        title,"path": slug.current,"body": pt::text(body)
      }`,},(document) => {
      console.log(document);
      return document;
    },(document) => {
      if (document.hasOwnProperty("isHidden")) {
        return !document.isHidden;
      }
      return true;
    }
  );
  return sanityAlgolia
    .webhookSync(sanityClient,req.body)
    .then(() => res.status(200).send("ok"));
}

和我从理智的帖子模式:

export default {
  name: 'post',title: 'Post',type: 'document',fields: [
    {
      name: 'title',title: 'Title',type: 'string',{
      name: 'slug',title: 'Slug',type: 'slug',options: {
        source: 'title',maxLength: 96,{
      name: 'author',title: 'Author',type: 'reference',to: {type: 'author'},{
      name: 'mainImage',title: 'Main image',type: 'image',options: {
        hotspot: true,{
      name: 'categories',title: 'Categories',type: 'array',of: [{type: 'reference',to: {type: 'category'}}],{
      name: 'publishedAt',title: 'Published at',type: 'datetime',{
      name: 'body',title: 'Body',type: 'blockContent',{
      name: 'extra',title: 'extra',],preview: {
    select: {
      title: 'title',author: 'author.name',media: 'mainImage',prepare(selection) {
      const {author} = selection
      return Object.assign({},selection,{
        subtitle: author && `by ${author}`,})
    },}

解决方法

sanity api v1 不适用于函数,我正在使用

const sanityClient = client({
  dataset: "production",useCdn: true,projectId: "project_id",});

默认为 v1,但为了使用函数,我添加了一个 apiVersion 参数,这使它使用了更高版本的 api:

const sanityClient = client({
  dataset: "production",projectId: "9dz8b3g1",apiVersion: "2021-03-25",});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。