dstotijn / go-notion

Go client for the Notion API.
MIT License
380 stars 39 forks source link

Update database entry Title #23

Closed fmartingr closed 2 years ago

fmartingr commented 2 years ago

Hello, I'm trying to update a database entry title field (Name in my case) but I don't manage to do so. If I use Name on the properties it told me to use the Title, and using the Title does nothing (no errors). I can update other properties without issue.

sample code:

name := "New name"

page, err := client.UpdatePage(ctx, "<pageid>", notion.UpdatePageParams{
    Title: []notion.RichText{
        {
            PlainText: name,
            Text: &notion.Text{
                Content: product.Name,
            },
        },
    },
)

I've tried several combinations, but nothing seems to being able to update the page title. I tried using "Title" as the field name (in case it was some JSON hardcoding) but that didn't work either.

Thanks for your work on this!

fmartingr commented 2 years ago

Managed to update it renaming the property to Title and using the following code:

page, err := client.UpdatePage(ctx, "id", notion.UpdatePageParams{
    DatabasePageProperties: &notion.DatabasePageProperties{
        "Title": notion.DatabasePageProperty{
            Type: notion.DBPropTypeTitle,
            Title: []notion.RichText{
                {
                    PlainText: name,
                    Text: &notion.Text{
                        Content: product.Name,
                    },
                },
            },
        },
    },
})

It didn't work having the property set as Name using the following:

"Name": notion.DatabasePageProperty{
    Type: notion.DBPropTypeTitle,
    Title: []notion.RichText{
        {
            PlainText: name,
            Name: "Name",
            Text: &notion.Text{
                Content: product.Name,
            },
        },
    },
}

// nor using
                "Title": notion.DatabasePageProperty{
                    Type: notion.DBPropTypeTitle,
                    Name: "Name",
dstotijn commented 2 years ago

The snippet from your first post should work (although you don't need to provide the PlainText field; it's read-only and not used by the API when updating rich text objects). But perhaps product.Name is not the same string value as name? Thus, I'd advise to test with this snippet:

name := "New name"

page, err := client.UpdatePage(ctx, "<pageid>", notion.UpdatePageParams{
    Title: []notion.RichText{
        {
            Text: &notion.Text{
                Content: name,
            },
        },
    },
})
fmartingr commented 2 years ago

Yeah sorry @dstotijn , I modified the code (adding name) to provide an example but failed to replace product.Name; in that case product.Name is a string and contains something, and it still didn't work. (Just tried it out again).

dstotijn commented 2 years ago

Strange; I can't reproduce the issue myself. When I test (with below test program) I'm able to update a page title, both pages with a workspace parent type and with a database parent type.

Could you maybe try testing with below program?

package main

import (
    "context"
    "errors"
    "flag"
    "fmt"
    "log"
    "os"

    "github.com/davecgh/go-spew/spew"
    "github.com/dstotijn/go-notion"
)

func main() {
    if err := run(); err != nil {
        log.Fatal(err)
    }
}

func run() error {
    ctx := context.Background()

    var pageID string
    flag.StringVar(&pageID, "page-id", "", "Notion page ID. (Required)")
    flag.Parse()

    if pageID == "" {
        flag.Usage()
        return errors.New("flag `-page-id` must be set")
    }

    apiKey := os.Getenv("NOTION_API_KEY")
    if apiKey == "" {
        return errors.New(`Environment variable "NOTION_API_KEY" must be set.`)
    }

    client := notion.NewClient(apiKey)

    params := notion.UpdatePageParams{
        Title: []notion.RichText{
            {
                Text: &notion.Text{
                    Content: "Updated title",
                },
            },
        },
    }

    updatedPage, err := client.UpdatePage(ctx, pageID, params)
    if err != nil {
        return fmt.Errorf("failed to update page: %w", err)
    }

    spew.Dump(updatedPage)

    return nil
}
fmartingr commented 2 years ago

That code works well for be (using any name on the Title database page property), after a few tests, it seems that providing DatabasePageProperties along with the Update is causing to ignore the Title attribute (I trimmed that from my first posts because I tought it was ireelevant). Am I unable to use the Title if I provide my own DatabasePageProperties?

I'm not sure if this is intended or not, but is a bit unintuitive for me.

dstotijn commented 2 years ago

(...) it seems that providing DatabasePageProperties along with the Update is causing to ignore the Title attribute

Ah, nice find!

I'm not sure if this is intended or not, but is a bit unintuitive for me.

That's not intended. I'll fix this sometime later this week.

fmartingr commented 2 years ago

Thank you for the fix @dstotijn ! Now I only need Notion to allow file uploads via API 😬