如何在 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},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},item_name: "test item",column_values: ${columnValues}
      )
        {
          id
        }
      }`
    ).then(res => {
      if(res.data){
        console.log('new item info: ',res.data)
      };
    });

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

解决方法

问题可能出在您的 GraphQL 查询上。要在星期一创建项目,您需要提供 column_values。不幸的是,在 Monday API 文档中并没有明确说明应该如何完成。您需要如何将 column_values 提供给 create_item 查询的答案可以在 Monday API documentationChanging column values with 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}}

请注意,在 query 变量中,我使用了 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);
});