首页 > 解决方案 > 如何在 Monday.com 中使用列值创建新的板项目?代码不工作

问题描述

我在 React 引导应用程序中使用 Monday.com API。

我可以使用项目名称成功创建一个新的板项目...

monday.api(
    `mutation {
        create_item (
          board_id: ${myBoardId}, 
          group_id: "new_group", 
          item_name: "new item creation",
        )
        {
          id
        }
      }`
 )

...但是当我尝试添加其他列值时,我收到 POST 500 错误。

monday.api(
    `mutation {
        create_item (
          board_id: ${myBoardId}, 
          group_id: "new_group", 
          item_name: "new item creation",
          column_values: {
            person: 00000000,
          }
        )
        {
          id
        }
      }`
 )

我试过为列值传递一个字符串......

let columnValues = JSON.stringify({
          person: 00000000,
          text0: "Requestor name",
          text9: "Notes",
          dropdown: [0],
        })

    monday.api(
      `mutation {
        create_item (
          board_id:${myBoardId}, 
          group_id: "new_group", 
          item_name: "test item",
          column_values: ${columnValues}
      )
        {
          id
        }
      }`
    ).then(res => {
      if(res.data){
        console.log('new item info: ', res.data)
      };
    });

...但没有创建任何项目,我没有收到任何错误,也没有任何日志。

标签: reactjsmonday.com

解决方案


问题可能出在您的 GraphQL 查询上。要在星期一创建项目,您需要提供column_values。不幸的是,在星期一的 API 文档中并没有明确说明应该如何完成。可以在Monday API 文档的使用 JSON 更改列值部分中找到如何将column_values 配置create_item查询的答案

请尝试以下代码:

const board_id = "<board_id>"
const group_id = "<group_id>"
const person_id = "<person_id>"
const item_name = "<item name>"

let query = `mutation { create_item (board_id:${board_id},group_id:  \"${group_id}\",item_name: \"${item_name}\",column_values: \"{\\\"person\\\":\\\"${person_id}\\\"}\"){id}}`

monday.api(query).then((res) => {
     console.log(res);
})

在哪里,

  • <board_id> - 你的董事会 ID
  • <group_id> - 你的组 ID
  • <item_name> - 您要创建的项目的名称
  • <person_id> - 用户 ID

如果您使用console.log查询,您应该会看到如下内容:

mutation { create_item (board_id:1293656973,group_id: "group_1",item_name: "New Item",column_values: "{\"person\":\"14153685\"}"){id}}

请注意,在查询变量中,我使用的是String Interpolation。所以字符串应该以`符号开始和结束

您还可以随时转储您的 GraphQL 查询并使用Monday API Try-It own tool 在线测试它们


推荐阅读