1

Both the index and key for each property_name from a database query are dynamic. The only constant is the type: "title" for the title property. The title itself is also nested in an array. I've marked it as I NEED THIS in the schema below. Furthermore, only one property is type: "title".

I have tried just about every object and array prototype function in various combinations , but due to the nested objects and arrays, and the property_name index and key being unknown, I cant figure it out.

My desired result would be to obj.map() the plain_text title and page_id of each page in the result to a JSX object.

Schema:

{
    object
    results [
        {
            object: page
            page_id: 442k3j423j4hk23kjh423
            created_time
            last_edited
            parent {
                type
                database_id 
            }
            archived 
            url
            properties {
                property_name {
                    id
                    type: 'title'
                    title [
                        plain_text: 'I NEED THIS'
                        id
                    ]
                }
                property_name {
                    id
                    type
                }
                property_name {
                    id
                    type
                }
            }
        },
        {
            object: page
            id
            created_time
            last_edited
            parent {
                type
                database_id 
            }
            archived 
            url
            properties {
                property_name {
                    id
                    type
                }
                property_name {
                    id
                    type
                }
                property_name {
                    id
                    type
                }
            }
        },
    ]

}

Desired resulting object for mapping:

var pages {
    page1: {
        page_id: '23j4lk23j4lk23j',
        page_title: 'This is a title'
    }
    page1: {
        page_id: '23j4lk23j4lk23j',
        page_title: 'This is a title'
    }
}
Eli
  • 11
  • 3

3 Answers3

0

Understand the Below Inner Map Concept and implement in your Code

First of all,To Access the Main Datas lets Assume,

const handleData = res.data

Here you need the page_id and plain_text, To fetch those data... Do some inner map methods like this

handleData.map((item)=> (
     item.results.map((inneritem)=> ( <p> {inneritem.page_id} </p> 
    inneritem.title.map((nesteditem)=> ( <p> {nesteditem.plain_text} </p> ) )
    ))
))
igmani
  • 176
  • 7
  • This would work great if each page only had one property, but each page has multiple properties, but only one property is the title and contains the title[] array. – Eli Aug 24 '21 at 19:43
0

I use notion_client for interaction with the site. This snippet getting a page and print title

client = notion_client.Client(auth=notion_token)
response = client.databases.retrieve(notion_database_id)
print(response['title'][0]['plain_text'])

The notion has bad API and documentation

AnZeky
  • 142
  • 1
  • 5
  • 2
    I know how to get info from a single page, as the title is a top level key when you request a single page, the problem arises only in querying a database (to list every page in a database), as every page in the resulting list has the title listed underneath a property object, which is dynamic, so cant be retrieved with normal indexing or .key notation. I could do a .map() and submit an API request for each page I get from the DB Query, but that is potentially hundreds of API requests for a single DB Query, which is ridiculous when I already have the data, just don't know how to get to it. – Eli Aug 24 '21 at 22:40
  • @Eli were you able to find the solution for this? Had high hopes with Notion API, but turns out to be really difficult when it comes to implementing – Will Kim Aug 01 '22 at 21:49
0

Here is the solution:

async function getPostsTitle(){
      const response = await notion.databases.query({
        database_id: 'your database id'
      }) 
      return response.results.map(post => post.properties.Name.title.map(item => item.plain_text)); 
    }