sanity-io / sanity-algolia

Utilities for indexing Sanity documents in Algolia
MIT License
67 stars 16 forks source link

import_sanity_algolia.default is not a function #31

Open antiantivirus opened 2 years ago

antiantivirus commented 2 years ago

Deploying serverless function on netlify and have been struggling to get sanity-algolia to work. Have tried adapting example on here as well as on the algolia tutorial.

Current error:

ERROR Invoke Error {"errorType":"TypeError","errorMessage":"(0 , import_sanity_algolia.default) is not a function","stack":["TypeError: (0 , import_sanity_algolia.default) is not a function"," at Runtime.handler (/var/task/frontend/netlify/functions/algolia-sync.js:12868:59)"," at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1028:29)"]}

And the code that I am running

import algoliasearch from 'algoliasearch'
import sanityClient, { SanityDocumentStub } from '@sanity/client'
import indexer from 'sanity-algolia'

const algolia = algoliasearch()
const sanity = sanityClient({

})

 export async function handler(event) {
  console.log(event.body)

  const sanityAlgolia = indexer(
    {
      product: {
        index: algolia.initIndex('products'),
      },
    },
    document => {
      switch (document._type) {
        case 'product':
          return {
            title: document.title,
          };
        default:
          return document
      }
    }
  );

  sanityAlgolia
    .webhookSync(sanity, event.body)
    .then(() => {
      return {
        statusCode: 200,
        body: JSON.stringify({ message: "All is good" }),
      };
    })
}

Unsure exactly why this error is occuring as sanity webhook is sending the correct data through. I previously had a GROQ projection but removed to keep the code simpler and isolate the error, just trying to change the title of the document a the moment so I know its working correctly.