From 65f6995a95605662f0d43942c662de14779cc1c3 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 2 Mar 2022 16:31:38 +0800 Subject: [PATCH] =?UTF-8?q?iterater=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getNotionData.js | 2 ++ pages/search/[keyword].js | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index dd26937e..62691554 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -109,11 +109,13 @@ async function getCustomNav ({ notionPageData }) { * @returns {undefined} */ function getTagOptions (schema) { + if (!schema) return {} const tagSchema = Object.values(schema).find(e => e.name === 'tags') return tagSchema?.options || {} } function getCategoryOptions (schema) { + if (!schema) return {} const categorySchema = Object.values(schema).find(e => e.name === 'category') return categorySchema?.options || {} } diff --git a/pages/search/[keyword].js b/pages/search/[keyword].js index c3bfaeff..870071b4 100644 --- a/pages/search/[keyword].js +++ b/pages/search/[keyword].js @@ -29,7 +29,7 @@ function appendText (sourceTextArray, targetObj, key) { * @returns */ function getTextContent (textArray) { - if (typeof textArray === 'object') { + if (typeof textArray === 'object' && isIterable(textArray)) { let result = '' for (const textObj of textArray) { result = result + getTextContent(textObj) @@ -40,6 +40,13 @@ function getTextContent (textArray) { } } +/** + * 对象是否可以遍历 + * @param {*} obj + * @returns + */ +const isIterable = obj => obj != null && typeof obj[Symbol.iterator] === 'function' + export async function getServerSideProps ({ params: { keyword } }) { const { allPosts, @@ -67,8 +74,7 @@ export async function getServerSideProps ({ params: { keyword } }) { indexContent = appendText(indexContent, properties, 'caption') }) } - console.log('搜索是否命中缓存', page !== null, indexContent) - + // console.log('搜索是否命中缓存', page !== null, indexContent) post.results = [] let hitCount = 0 const re = new RegExp(`${keyword}`, 'gim')