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

Type checking issue when querying a database: properties field #527

Open
RichEwin opened this issue Sep 30, 2024 · 2 comments
Open

Type checking issue when querying a database: properties field #527

RichEwin opened this issue Sep 30, 2024 · 2 comments

Comments

@RichEwin
Copy link

Report bugs here only for the Node JavaScript library.

If you're having problems using Notion's API, or have any other feedback about the API including feature requests for the JavaScript library, please email support at [email protected].

Describe the bug
When querying a database the response types are suggesting that 'properties' does not exist on 'PartialPageObjectResponse'

Property 'properties' does not exist on type 'PageObjectResponse | PartialPageObjectResponse | PartialDatabaseObjectResponse | DatabaseObjectResponse'.
  Property 'properties' does not exist on type 'PartialPageObjectResponse'
 const response = await notionClient.databases.query({
      database_id: databaseId,
      filter: {
        property: marketCode,
        select: {
          equals: market?.toLowerCase(),
        },
      },
    });

    const results = response.results[0]?.properties;

Below is the response when querying the database; you can see properties exist.

{
  object: 'page',
  id: '10...',
  created_time: '2024...',
  last_edited_time: '2024...',
  created_by: { object: 'user', id: '48e6f...' },
  last_edited_by: { object: 'user', id: '48e6...' },
  cover: null,
  icon: null,
  parent: {
    type: 'database_id',
    database_id: '10d...'
  },
  archived: false,
  in_trash: false,
  properties: {
    'Market...': { id: 'BMOO', type: 'select', select: [Object] },
    'Market...': { id: 'G%3DDn', type: 'select', select: [Object] },
    ...
  },
  url: 'https://www.notion.so/...',
  public_url: null
}

Is there a way to handle this, instead of me manually defining the response which seems unnecessary.

To Reproduce
Node version:
Notion JS library version:

Steps to reproduce the behavior:

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
Please include any screenshots that help explain your problem.

Additional context
Add any other context about the problem here.

@feliceforgione
Copy link

When querying database using a filter, I receive a "body failed validation" error

APIResponseError: body failed validation. Fix one:
body.filter.or should be defined, instead was `undefined`.
body.filter.and should be defined, instead was `undefined`.
body.filter.title should be defined, instead was `undefined`.
body.filter.rich_text should be defined, instead was `undefined`.
body.filter.number should be defined, instead was `undefined`.
body.filter.checkbox should be defined, instead was `undefined`.
body.filter.select should be defined, instead was `undefined`.
body.filter.multi_select should be defined, instead was `undefined`.
body.filter.status should be defined, instead was `undefined`.
body.filter.date should be defined, instead was `undefined`.
body.filter.people should be defined, instead was `undefined`.
body.filter.files should be defined, instead was `undefined`.
body.filter.url should be defined, instead was `undefined`.
body.filter.email should be defined, instead was `undefined`.
body.filter.phone_number should be defined, instead was `undefined`.
body.filter.relation should be defined, instead was `undefined`.
body.filter.created_by should be defined, instead was `undefined`.
body.filter.created_time should be defined, instead was `undefined`.
body.filter.last_edited_by should be defined, instead was `undefined`.
body.filter.last_edited_time should be defined, instead was `undefined`.
body.filter.formula should be defined, instead was `undefined`.
body.filter.unique_id should be defined, instead was `undefined`.
body.filter.rollup should be defined, instead was `undefined`.
    at async ProjectsList (./src/app/components/ProjectsList.tsx:23:35)
digest: "716658379"
const { results } = await notion.databases.query({
    database_id: databaseId!,
    filter: {
      property: "Tags",
      contains: "AI",
    }
  });
  console.log(results);

Below is the response when querying the database; you can see properties exist.

[
{
  object: 'page',
  id: '039c4ca9-e237-481a-9581-',
  created_time: '2024-10-01T20:21:00.000Z',
  last_edited_time: '2024-10-05T00:35:00.000Z',
  created_by: { object: 'user', id: 'f65771fb-5ea1-4e48-b963-' },
  last_edited_by: { object: 'user', id: 'f65771fb-5ea1-4e48-b963-' },
  cover: null,
  icon: null,
  parent: {
    type: 'database_id',
    database_id: '112794ea-af04-8071-8225-'
  },
  archived: false,
  in_trash: false,
  properties: {
    'Current Versions': [Object],
    'Github Repos': [Object],
    Directory: [Object],
    'Main Run Command': [Object],
    'Short Description': [Object],
    Tags: [Object],
    Name: [Object]
  },
  url: 'https://www.notion.so/....',
  public_url: null
}
]

Node version: 20.17.0
Notion JS library version: v2.2.15

I also have typescript issues when trying to access the custom properties. It seems like the typescript definitions are not accurate when it comes to properties.

@mattrossman
Copy link

See section Type Guards from the README. These narrow types for the use case you show.

TypeScript Playground with an example of safely reading .properties from a query result.

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