makenotion / notion-sdk-js

Official Notion JavaScript Client
https://developers.notion.com/docs
MIT License
4.95k stars 591 forks source link

How do I get value from blocks.retrieve #485

Closed XHXIAIEIN closed 10 months ago

XHXIAIEIN commented 10 months ago

I am learning how to use notion API.

import { Client } from "@notionhq/client";
import { config } from "dotenv"

config()

async function main() {

  const NOTION_TOKEN = process.env.NOTION_TOKEN
  const NOTION_PAGE_ID = process.env.NOTION_PAGE_ID
  const NOTION_DATABASE_ID = process.env.NOTION_DATABASE_ID

  const notion = new Client({
    auth: NOTION_TOKEN,
  })

  const block = await notion.blocks.retrieve({
    block_id: NOTION_PAGE_ID || "",
  });

  console.log(block)

}

main()
  .then(() => process.exit(0))
  .catch((err) => {
    console.error(err);
    process.exit(1);
  });

I have no problem running this, I can return a JSON object.

{
  object: 'block',
  id: '19358c4d-f341-4e62-8db0-3805e8d552bb',
  parent: { type: 'workspace', workspace: true },
  created_time: '2024-01-24T07:15:00.000Z',
  last_edited_time: '2024-01-24T19:48:00.000Z',
  created_by: { object: 'user', id: 'c81623cb-b097-4ab7-845a-29c28411cb84' },
  last_edited_by: { object: 'user', id: 'c81623cb-b097-4ab7-845a-29c28411cb84' },
  has_children: true,
  archived: false,
  type: 'child_page',
  child_page: { title: 'Next.js Notion Starter Kit Template' },
  request_id: '06287559-75f9-441f-abf9-b1780f5fa62a'
}

But if I want to directly output the values, I will throw an error

  console.log(block.child_page.title)
PS D:\Project\Github\notion-sdk-typescript-starter> npm start

> notion-sdk-project@1.0.0 start
> ts-node src/index.ts

D:\Project\Github\notion-sdk-typescript-starter\node_modules\ts-node\src\index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/index.ts:22:21 - error TS2339: Property 'child_page' does not exist on type 'GetBlockResponse'.
  Property 'child_page' does not exist on type 'PartialBlockObjectResponse'.

22   console.log(block.child_page.title)
                       ~~~~~~~~~~

    at createTSError (D:\Project\Github\notion-sdk-typescript-starter\node_modules\ts-node\src\index.ts:859:12)
    at reportTSError (D:\Project\Github\notion-sdk-typescript-starter\node_modules\ts-node\src\index.ts:863:19)
    at getOutput (D:\Project\Github\notion-sdk-typescript-starter\node_modules\ts-node\src\index.ts:1077:36)
    at Object.compile (D:\Project\Github\notion-sdk-typescript-starter\node_modules\ts-node\src\index.ts:1433:41)
    at Module.m._compile (D:\Project\Github\notion-sdk-typescript-starter\node_modules\ts-node\src\index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Object.require.extensions.<computed> [as .ts] (D:\Project\Github\notion-sdk-typescript-starter\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) {
  diagnosticCodes: [ 2339 ]
}
PS D:\Project\Github\notion-sdk-typescript-starter> 

Even if I try to convert it to JSON, it won't work

  const block = await notion.blocks.retrieve({
    block_id: NOTION_PAGE_ID || "",
  });

  const data = await block.json()
  console.log(data.child_page.title)

I don't know what I should do.