How Can I Use the Notion Object in Functions Outside defineComponent in the Default Notion/Pipedream Connection?

This topic was automatically generated from Slack. You can find the original thread here.

Struggling to get my connection working. I used to use a custom integration but moved to the default Notion / Pipedream connection. The default connection “template” is below. I’m unable to determine how to use the notion object in functions outside defineComponent. I tried moving all my functions within defineComponent but and having run() call main but that does not work. I think the layout below is proper but I do not know the syntax or I’m missing an import statement, etc.
Thanks,
-Rich

import { axios } from "@pipedream/platform"
export default defineComponent({
props: {
notion: {
type: "app",
app: "notion",
}
},
async run({steps, $}) {
return await axios($, {
url: https://api.notion.com/v1/users/me,
headers: {
Authorization: Bearer ${this.notion.$auth.oauth_access_token},
"Notion-Version": 2021-08-16,
},
})
},
})
async function main () {
notion.database... << undefined
this.notion.database... << undefined
defineComponent.props.notion.database... << undefined
this.props.notion.app.database... << undefined
steps.notion << undefined
}
await main();

Hi Richard, you’d have to keep the code inside the defaultComponent export. You can put it inside the run function or add methods to reuse methods (see docs). Could you share your use case for writing custom code using the template and not the available registry components?

I’m working on migrating to that but not finding easy ways post querying a database to loop and create multiple pages based on the query results.

But thank you for the advice above. “this.notion” now working however, this.main() called from the run() method is throwing an exception that main is not a function. When I moved main into define component I had to drop async function main() to just main. Now when I call using this.main(); from within run() to kick off the process I receive the errors. Thoughts?

Gotcha, makes sense. Looping is the next big feature we will release, so stay tuned on that!

Re: the main function, let me share you an example how it should look like

export defaultComponent({
    ...metadata,
    props: { ...props },
    methods: {
        method1() { ... }.
        async method2() {
            this.method1() // also accessible here
        },
    },
    async run({}) {
        this.method1() // call method here
        await this.method2()
    }
})

Thanks! I’m much closer now. Will the following ensure the notion object exists prior to calling this.handlerRecurringTasks()? Right now that function calls this.notion.databases.query and it appears it’s not defined. Anything else I should be doing?
async run({ steps, $ }) {
await axios($, {
url: https://api.notion.com/v1/users/me`,`
headers: {
Authorization: Bearer ${this.notion.$auth.oauth_access_token},
"Notion-Version": 2022-06-28,
},
})
await this.handleRecurringTasks();
},

The notion object that is being exposed to you there is the Notion authentication app prop, and not exactly the notion sdk library. Check out lesson 1.4 from Pipedream University to understand what is this app prop and how you can use it

You can leverage Pipedream’s authentication handling by accessing the oauth_access_token and inject it to the Notion SDK client just like we do here. With this client, you can then access the objects and functions from the official Notion API

The link above is for our components repository where we store the source code for our registry components. You can see the code for our Notion components in this link.

Example for the databases query: https://github.com/PipedreamHQ/pipedream/blob/master/components/notion/notion.app.mjs#L225
Notice how it’s using the Notion Client imported from @notionhq/client

Thank you so much!

Added a method for _getNotionClient(). Now calling the “notion object” using: const response = await this._getNotionClient().databases.query({…}).

Last run I received this error: pnpm install --prefer-offline --ignore-scripts -P and it looks like I’m trying to install something but it failed.

Error: Command failed: pnpm install --prefer-offline --ignore-scripts -P

    at __node_internal_genericNodeError (node:internal/errors:865:15)
    at ChildProcess.exithandler (node:child_process:422:12)
    at ChildProcess.emit (node:events:517:28)
    at maybeClose (node:internal/child_process:1098:16)
    at ChildProcess._handle.onexit (node:internal/child_process:303:5)

Disregard last…typo in import statement!

Hmmm, receiving this error from _getNotionClient method: Cannot read properties of undefined (reading ‘oauth_access_token’)

I’m up.

Hi can you paste here your code? It’ll be easier to troubleshoot