Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
gnslalsl12 opened this issue Jan 31, 2024 · 6 comments
Open
2 tasks done

Comments

@gnslalsl12
Copy link

gnslalsl12 commented Jan 31, 2024

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
Copy link
Author

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

@Curt-Park
Copy link

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

@gnslalsl12
Copy link
Author

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
Copy link

@gnslalsl12
Copy link
Author

@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
Copy link

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

Kimhan-nah added a commit to Kimhan-nah/morethan-log that referenced this issue Jun 4, 2024
Sooamazing added a commit to Sooamazing/morethan-log that referenced this issue Aug 3, 2024
- reference PR [Bug]: 100개 이상의 글 블록을 가져오지 못하는 문제 morethanmin#333
- fixed at local (part: block[id].value? )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants