调用Algolia saveObjects报错:index.saveObject不是函数
问题:Algolia saveObjects 方法报错 "not a function"
我从API获取数据后存入Algolia,以便使用Algolia提供的Instant Search组件,但运行以下代码时出现错误:
const searchClient: any = algoliasearch( '', //Application ID '' //API Key ); const index: any = searchClient.initIndex('hackernews_search'); const [newsData, setNewsData] = useState<News[]>([]); const [currentPage, setCurrentPage] = useState<number>(0); useEffect(() => { const getCurrentPageData = async () => { let currentNewsData : News[] = []; const response = await axios.get(`https://hn.algolia.com/api/v1/search_by_date?tags=story&hitsPerPage=30&page=${currentPage}`) response.data.hits.map((result:any) => { let newsObject :News = { title: result.title, url: result.url, points: result.points, } currentNewsData.push(newsObject) }) setNewsData(currentNewsData); index.saveObjects(currentNewsData); } getCurrentPageData(); },[currentPage])
错误信息:
TypeError: index.saveObject is not a function
at getCurrentPageData (Home.tsx:37:1)
我是按照官方文档操作的,不清楚问题所在,希望得到建议。
已解决问题:导入错误
解决方案
报错原因是导入了仅提供搜索方法的algoliasearch/lite包,而saveObjects是写入方法,因此报错。改为导入完整包import algoliasearch from 'algoliasearch'后问题解决。
内容的提问来源于stack exchange,提问作者archie09
相关产品推荐
相关产品推荐

