使用 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",});

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...