为什么我无法索引到 Algolia?

Why did I fail to index to Algolia?

我正在按照 gatsby tutorial 安装 algolia。做完 gatsby build 我得到

TypeError: Cannot read property 'addObjects' of undefined

我的src/utils/algolia.js文件

  const postQuery = `{
    posts: allMarkdownRemark(
      filter: { fileAbsolutePath: { regex: "/content/" } }
    ) {
      edges {
        node {
          objectID: id
          frontmatter {
            title
            date(formatString: "DD MMMM, YYYY")
          }
          excerpt(pruneLength: 5000)
        }
      }
    }
  }`

  const flatten = arr =>
    arr.map(({ node: { frontmatter, ...rest } }) => ({
      ...frontmatter,
      ...rest,
    }))
  const settings = { attributesToSnippet: [`excerpt:20`] }

  const queries = [
    {
      query: postQuery,
      transformer: ({ data }) => flatten(data.posts.edges),
      indexName: `Posts`,
      settings,
    },
  ]

  module.exports = queries

我按照基本教程进行了最少的定制。我究竟做错了什么?

我的github

当您指定的索引不存在时会发生这种情况。

在您的情况下,您发布的代码指定使用名为 Posts 的索引,例如:

indexName: `Posts`,

gatsby algolia 插件将尝试找到该索引并推送到它。如果它不存在,它将失败并出现这个相当神秘的错误。

为了修复它,请登录您的 Algolia 仪表板并使用该名称创建一个索引。