如何在带有列值的 Monday.com 中创建新的看板项目?代码不工作

How do I create a new board item in Monday.com w/ column values? Code is not working

我在 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)
      };
    });

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

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

的 JSON 部分的值

请尝试以下代码:

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);
})

其中,

  • - 你的看板 ID
  • - 您的群组 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 yourself tool

在线测试它们

解决方法如下:

const variables = ({
    boardId : 00000000,
    groupId: "new_group",
    itemName : "New Item",
    columnValues: JSON.stringify({
        people78: { 
            personsAndTeams: [
            {
                id: 00000000, 
                kind: "person"
            }
            ] 
        },
        text0: "Yosemite Sam",
        dropdown: {
            labels: [
            "TAM"
            ]
        },
    })
});

const query = `mutation create_item ($boardId: Int!, $groupId: String!, $itemName: String!, $columnValues: JSON!) { 
    create_item (
        board_id: $boardId,
        group_id: $groupId,
        item_name: $itemName, 
        column_values: $columnValues
    ) 
    { 
        id
    } 
}`;

monday.api(query, {variables}).then((res) => {
    console.log('new item info: ', res);
});