使用 Trello API 在卡片上设置自定义字段值 [英] Setting custom field values on a card with the Trello API

查看:66
本文介绍了使用 Trello API 在卡片上设置自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Trello API 的新自定义字段方法 设置卡片上自定义字段的值.

I am trying to use the new Custom Fields methods of the Trello API to set the value of a custom field on a card.

我创建了一个 number 类型的自定义字段.当我通过它的 id 对自定义字段发出 GET 请求时,它返回这个自定义字段对象:

I've created a custom field of type number. When I make a GET request for the custom field by it's id, it returns this custom field object:

{
"id":"5ab13cdb8acaabe576xxxxx",
"idModel":"54ccee71855b401132xxxxx",
"modelType":"board",
"fieldGroup":"837a643fd25acc835affc227xxxxxxxxxxxxxxxxxxxx",
"name":"Test Number Field",
"pos":16384,
"type":"number"
}

然后,当我在 Trello UI 中创建一张新卡片时(但不要在 Test Number Field 框中输入值),然后使用 GET 该卡片customFieldItems=true(如here),它返回这个卡片对象(删除了不相关的字段):

Then when I create a new card in the Trello UI (but do not type in a value in the Test Number Field box) and then GET that card using the customFieldItems=true (as documented here), it returns this card object (irrelevant fields removed):

{
"id": "5ab56e6b62abac194d9xxxxx",
"name": "test1",
"customFieldItems": []
}

请注意,由于我没有在 UI 的 Test Number Field 框中输入任何内容,customFieldItems 属性包含一个空白数组.

Note that since I did not type anything into the Test Number Field box in the UI, the customFieldItems property contains a blank array.

然后,如果我在 UI 的 Test Number Field 框中输入数字 1 并再次GET 卡片,它会返回这个(删除了不相关的字段):

Then if I type in the number 1 in the Test Number Field box in the UI and GET the card again, it returns this (irrelevant fields removed):

{
"id": "5ab56e6b62abac194d9xxxxx",
"name": "test1",
"customFieldItems":
    [
        {
        "id": "5ab570c5b43ed17b2dxxxxx",
        "value": {
            "number": "1"
            },
        "idCustomField": "5ab13cdb8acaabe5764xxxxx",
        "idModel": "5ab56e6b62abac194d9xxxxx",
        "modelType": "card"
        }
    ]
}

我希望能够通过 API 设置此自定义字段的值.

I want to be able to set the value of this custom field via API.

当我转到 设置、更新和删除卡片上自定义字段的值"的 API 文档时, (此处) 我插入以下信息:

When I go to the API documentation for "Setting, updating, and removing the value for a Custom Field on a card," (here) I plug in the following information:

查询身份验证

  • key:(我们的有效/可用的 Trello API 密钥)

令牌:(我们的有效/可用的 Trello API 令牌)

路径参数

  • idCard:(应为其设置/更新自定义字段值的卡片的 ID) 5ab56e6b62abac194d9xxxxx

idCustomField (卡片上自定义字段的 ID.):5ab570c5b43ed17b2dxxxxx

查询参数

  • idCustomField(商品所属的自定义字段的 ID.):5ab13cdb8acaabe576xxxxx

  • idCustomField (ID of the Custom Field to which the item belongs.): 5ab13cdb8acaabe576xxxxx

modelType (这应该总是卡片.):card

value (包含要为卡片的自定义字段值设置的键和值的对象.用于设置值的键应与定义的自定义字段的类型相匹配.): {"number": 2}

当我点击 Try It 时,我得到响应:400 Bad RequestInvalid custom field item value."

When I click Try It, I get the response: 400 Bad Request "Invalid custom field item value."

我尝试了以下几点:

  • 切换两个 idCustomField 值(令人困惑的是路径参数和查询参数都具有相同的名称,这意味着它们旨在接受相同的值,但是它们具有不同的描述,并且描述含糊不清/令人困惑).

  • Switching the two idCustomField values (it's confusing that both the path parameter and query parameter have the same name, implying that they are meant to accept the same value, but then they have different descriptions, and the descriptions are vague/confusing).

将两个 idCustomField 值设置为相同的值(对于两个可能的 ID)

Setting both idCustomField values to the same thing (for both of the possible IDs)

设置值为 2, {"number": "2"}, {number: 2}, <代码>{number: "2"} 和更多.

Setting the value to 2, {"number": "2"}, {number: 2}, {number: "2"} and more.

无论我尝试什么,我总是得到无效的自定义字段项目值."无论卡片在自定义字段中是否有值,这都会以相同的方式运行.

No matter what I try, I always get "Invalid custom field item value." This behaves the same way whether the card has a value in the custom field or not.

我很确定路径参数中的 idCustomField 已被接受,因为当我更改一个字符时,它反而给我这个错误:idCustomField 的值无效".

I'm pretty sure that the idCustomField in the path params is being accepted, because when I change one character, it gives me this error instead: "invalid value for idCustomField".

所以我不知道 "Invalid custom field item value." 是指查询参数 idCustomField 还是 value.

So I don't know if the "Invalid custom field item value." is referring to the query param idCustomField or the value.

我也不知道卡片在自定义字段中是否具有现有值是否有区别,但我希望能够设置此自定义字段的值,无论它当前是否具有字段中的值.

I also don't know if it makes a difference whether or not the card has an existing value in the custom field, but I want to be able to set the value of this custom field regardless of whether or not it currently has a value in the field.

推荐答案

Trello 文档页面 是错误的.您应该使用使用 fetch 的下一个示例.

The live example (using XMLHttpRequest) on the Trello documentation page is wrong. You should use the next example using fetch.

var url = "https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?token={yourToken}&key={yourKey}";
var data = {value: { number: "42" }};
fetch(url, { body: JSON.stringify(data), method: 'PUT', headers: {'content-type': 'application/json'}})
.then((resp) => resp.json())
.then((data) => console.log(JSON.stringify(data, null, 2)))
.catch((err) => console.log(JSON.stringify(err, null, 2)))

这个例子有效.在尝试之后,我修改了 XMLHttpRequest 版本,它也有效.

This example works. And after trying this, I modified the XMLHttpRequest version and it worked too.

var data = null;
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

data = {value: { number: "3" }};  //added
var json = JSON.stringify(data);  //added

xhr.open("PUT", 'https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?key={yourkey}&token={yourtoken}');
xhr.setRequestHeader('Content-type','application/json');  //added
xhr.send(json);  //modified

重点是您应该 1) 将请求 Content-type 标头设置为 application/json 和 2) 通过JSON 对象主体.

The point is that you should 1) set the request Content-type header to application/json and 2) pass the value via JSON object body.

我尝试编辑文档中的实时示例,但我无法做到.我希望他们能尽快修复它.

I tried to edit the live example on the documentation but I it was not possible. I hope they will fix it soon.

这篇关于使用 Trello API 在卡片上设置自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆