morethanmin / morethan-log

😎 A static blog using notion database
https://morethan-log.vercel.app
MIT License
1.89k stars 1.51k forks source link

[Bug]: 100개 이상의 글 블록을 가져오지 못하는 문제 #333

Open gnslalsl12 opened 7 months ago

gnslalsl12 commented 7 months ago

Describe the bug

getPosts.ts의 코드에서 pageId는 노션에 있는 글 개수만큼 가져오는데, block이 100개를 넘어가는 글의 block은 가져오지 못해서

- error src\apis\notion-client\getPosts.ts (70:48) @ value
- error Error [TypeError]: Cannot read properties of undefined (reading 'value')
    at getPosts (webpack-internal:///./src/apis/notion-client/getPosts.ts:68:53)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getStaticProps (webpack-internal:///./src/pages/index.tsx:29:86) {
  digest: undefined
}
  68 |     const properties = (await getPageProperties(id, block, schema)) || null
  69 |     // Add fullwidth, createdtime to properties
> 70 |     properties.createdTime = new Date(block[id].value?.created_time).toString()
     |                                                ^
  71 |     properties.fullWidth =
  72 |       (block[id].value?.format as any)?.page_full_width ?? false
  73 |

이런 에러가 뜹니다. 때문에 block을 response.block 보다는 getBlocks를 사용해서 가져오면 100개가 넘는 글들의 블록도 정상적으로 가져오게 됩니다.

아래는 getPosts.ts를 원하는 방향으로 고쳐놓은 코드입니다. (tempBlock)

import { CONFIG } from "site.config"
import { NotionAPI } from "notion-client"
import { idToUuid } from "notion-utils"

import getAllPageIds from "src/libs/utils/notion/getAllPageIds"
import getPageProperties from "src/libs/utils/notion/getPageProperties"
import { TPosts } from "src/types"

/**
 * @param {{ includePages: boolean }} - false: posts only / true: include pages
 */

// TODO: react query를 사용해서 처음 불러온 뒤로는 해당데이터만 사용하도록 수정
export const getPosts = async () => {
  let id = CONFIG.notionConfig.pageId as string
  const api = new NotionAPI()

  const response = await api.getPage(id)
  id = idToUuid(id)
  const collection = Object.values(response.collection)[0]?.value
  const block = response.block
  const schema = collection?.schema

  const rawMetadata = block[id].value

  // Check Type
  if (
    rawMetadata?.type !== "collection_view_page" &&
    rawMetadata?.type !== "collection_view"
  ) {
    return []
  } else {
    // Construct Data
    const pageIds = getAllPageIds(response)
    const tempBlock = await (await api.getBlocks(pageIds)).recordMap.block

    const data = []
    for (let i = 0; i < pageIds.length; i++) {
      const id = pageIds[i]
      const properties =
        (await getPageProperties(id, tempBlock, schema)) || null
      if (!tempBlock[id]) continue

      // Add fullwidth, createdtime to properties
      properties.createdTime = new Date(
        tempBlock[id].value?.created_time
      ).toString()
      properties.fullWidth =
        (tempBlock[id].value?.format as any)?.page_full_width ?? false

      data.push(properties)
    }

    // Sort by date
    data.sort((a: any, b: any) => {
      const dateA: any = new Date(a?.date?.start_date || a.createdTime)
      const dateB: any = new Date(b?.date?.start_date || b.createdTime)
      return dateB - dateA
    })

    const posts = data as TPosts
    return posts
  }
}

To reproduce

  1. Go to ...
  2. Click on ...
  3. Scroll down to ..
  4. See error

Browser

google chrome v109, windows 11

Validations

gnslalsl12 commented 7 months ago

issue를 처음 써봐서 이렇게 하는 게 맞는지 모르겠네요! 일단 getPosts를 저렇게 고치면 100개가 넘는 글들도 잘 가져오게 됩니다. image

Curt-Park commented 7 months ago

How about pushing a pull request if you already have a solution?

gnslalsl12 commented 6 months ago

How about pushing a pull request if you already have a solution?

I want to but... I don't know how to push a pull request with only exact files or codes I've chose. There're a lot of changes in my forked morethan codes.

Curt-Park commented 6 months ago

@gnslalsl12 It's a good opportunity to commit your contribution. :)

fyi. https://docs.github.com/ko/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork

gnslalsl12 commented 6 months ago

@gnslalsl12 It's a good opportunity to commit your contribution. :)

fyi. https://docs.github.com/ko/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork

oh! thank you :) I'll give it a try and commit when I figure out how to do it. Thank you so much :D

jungsikjeong commented 6 months ago

@gnslalsl12 이거 수정이되었나용?