0

Imagine a database table with two columns, Name (title) and Collaborators (relation). Things work fine when I add a row with a single collaborator like so.

qveri <- list(
  parent = list(database_id = "database_id"),
  properties = list(
    Name = list(
      title = list(
        list(text = list(content = "klekele"))
      )
    ),   
    "Collaborators" = list(
      relation = list(
        list(id = "hash_collaborator_1"))
    )
  )
)
out <- POST(
  url = "https://api.notion.com/v1/pages", 
  add_headers("Authorization" = paste("Bearer", "notion_key"),
              "Notion-Version" = "2021-05-13"),
  body = qveri, 
  encode = "json"
)

> out$status_code
[1] 200

Things get tricky when I try to insert more than one collaborator into the field. The error message is validation_error but other than that, the spaghetti indicating where the error might be are pretty uninformative:

$message
[1] "body failed validation. Fix one:
\nbody.properties.Collaborators.title should be defined, instead was `undefined`.
\nbody.properties.Collaborators.rich_text should be defined, instead was `undefined`.
\nbody.properties.Collaborators.number should be defined, instead was `undefined`.
\nbody.properties.Collaborators.url should be defined, instead was `undefined`.
\nbody.properties.Collaborators.select should be defined, instead was `undefined`.
\nbody.properties.Collaborators.multi_select should be defined, instead was `undefined`.
\nbody.properties.Collaborators.people should be defined, instead was `undefined`.
\nbody.properties.Collaborators.email should be defined, instead was `undefined`.
\nbody.properties.Collaborators.phone_number should be defined, instead was `undefined`.
\nbody.properties.Collaborators.date should be defined, instead was `undefined`.
\nbody.properties.Collaborators.checkbox should be defined, instead was `undefined`.
\nbody.properties.Collaborators.relation should be defined, instead was `undefined`.
\nbody.properties.Collaborators.files should be defined, instead was `undefined`.
\nbody.properties.Collaborators.status should be defined, instead was `undefined`.
\nbody.properties.Name.id should be defined, instead was `undefined`.
\nbody.properties.Name.name should be defined, instead was `undefined`.
\nbody.properties.Name.start should be defined, instead was `undefined`."

While trying to insert more than one entry, I have tried various ways to replace the "Collaborators" value in the qveri variable, including but not limited to:

    Collaborators = list(
      relation = list(
        list(
          id = list(
            data.frame(id = c("hash_collaborator1",
                              "hash_collaborator2")
            )
          )
        )
      )
    )
    Collaborators = list(
      relation = list(
        data.frame(id = c("hash_collaborator1",
                          "hash_collaborator2")
        )
      )
    )
    Collaborators = list(
      relation = list(
        id = as.list(
          c("hash_collaborator1",
            "hash_collaborator2")
        )
      )
    )
    "Collaborators" = list(
      relation = list(
        list(id = list("hash_collaborator1",
                       "hash_collaborator2")))
    )

Any guidance where the documentation could be of help or on how to properly construct the query in order to insert more than one collaborator will be much appreciated.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197

1 Answers1

0

When I populate a table row by hand with multiple collaborators, the returned result yields that this is in a data.frame (see below).

> str(cnt$results$properties)
'data.frame':   1 obs. of  2 variables:
 $ Collaborators:'data.frame':  1 obs. of  3 variables:
  ..$ id      : chr "fake_id"
  ..$ type    : chr "relation"
  ..$ relation:List of 1
  .. ..$ :'data.frame': 3 obs. of  1 variable:
  .. .. ..$ id: chr  "hash_collaborator1" "hash_collaborator2" "hash_collaborator3"
 $ Name         :'data.frame':  1 obs. of  3 variables:
  ..$ id   : chr "title"
  ..$ type : chr "title"
  ..$ title:List of 1
  .. ..$ : list()

Reading the documentation and squinting really hard, I saw that the relation, in JSON format, should be an array.

enter image description here

This made me think that perhaps the array could translate to R's data.frame.

Lo and behold

    "Collaborators" = list(
      relation = data.frame(id = c("hash_collaborator1",
                                   "hash_collaborator2",
                                   "hash_collaborator3"))
    )

turns out to be the correct formulation of the object to be POST-ed.

> out$status_code
[1] 200
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197