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

查看:317
本文介绍了使用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框中键入值),然后使用customFieldItems=true GET将该卡(如记录的此处),返回此卡片对象(不相关的字段已删除):

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": []
}

请注意,由于我没有在用户界面的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.

然后,如果我在用户界面的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:

查询身份验证

  • 密钥:(我们有效/有效的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

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

当我单击尝试"时,得到响应:400 Bad Request "Invalid custom field item value."

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

我尝试了以下操作:

  • 切换两个 idCustomField 值(令人困惑的是path参数和query参数都具有相同的名称,这意味着它们旨在接受相同的值,但是它们具有不同的值说明,且说明含糊/令人困惑).

  • 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"}等.

无论我尝试什么,我总是得到"Invalid custom field item value.",无论卡片在自定义字段中是否具有值,其行为都相同.

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 将被接受,因为当我更改一个字符时,它会给我这个错误:"invalid value for 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 还是.

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对象主体传递value.

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天全站免登陆