Trello API:如何填充清单?

时间:2016-05-16 19:30:46

标签: trello

我成功创建了卡片和清单,但我无法使用所需的项目填充新创建的清单。 Trello确实以400“无效值名称”对POST做出回应 我在试图找到以下代码中的错误时变得疯狂。 有没有人能帮忙?

function CreateChecklist(card) { // this work fine
    var newList = 
        {
        idCard:card.id,
        name:"Cicli di lavorazione",
        pos:"top"
        }
    Trello.post('/checklists/', newList, CreateCheckListItems, t_error);
}


function CreateCheckListItems(checkList) { // 400 'invalid value for name'
    console.log("Checklist created (id="+checkList.id+"):");
    console.dir(checkList);
    //var newItem = "TESTNAME"; // WRONG version
    var newItem = {name:"TESTNAME"}; // SOLUTION! Thanks Casey
    Trello.post('/checklists/'+checkList.id+'/checkItems/', newItem, null, t_error);    
}

以下是包含示例数据的完整代码。 范围是在特定的Trello列表中创建卡片。 在每张卡片中都有一张包含生产线索的清单 (请更改Trello客户端密钥和LISTAid以使用代码)

<html>
<head>
    <link rel="stylesheet" type="text/css" href="creaSchede.css">
    <script src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="https://trello.com/power-ups/power-up.min.js"></script>
    <script src="https://api.trello.com/1/client.js?key=XXXXXXX-YOUR-KEY-XXXXXXXX"></script>
</head>
<body>
    <script type="text/javascript">//<![CDATA[
$(window).load(function(){

var ordini;
var LISTAid = "5739a2b4c5dc9d398b1c7143"; // <<<---------- PUT HERE YOUR LIST ID
var scheda = {};
var orp = {};
var fasi = {};


// TRELLO success on card creation
var tcard_success = function(successMsg) {
    scheda = {};
    scheda = successMsg;
    console.log("Creata scheda: "+scheda.url);
    console.dir(scheda);
    $("#output").append(function(n){
        return "<a class='board' id='"+ scheda.id +"'target='trello' href='" + scheda.url + "'>" + scheda.name + "</a>";
    });
    CreateChecklist(scheda);
};

// TRELLO error (generale)
var t_error = function(errorMsg) {
  console.log(errorMsg.responseText);
  console.log("Error msg data:")
  console.dir(errorMsg);
};


var onAuthorize = function() {
    updateLoggedIn();
    $("#output").empty();
    Trello.members.get("me", function(member){
        $("#fullName").text(member.fullName);
    });
};

var updateLoggedIn = function() {
    var isLoggedIn = Trello.authorized();
    $("#loggedout").toggle(!isLoggedIn);
    $("#loggedin").toggle(isLoggedIn);        
};

var logout = function() {
    Trello.deauthorize();
    updateLoggedIn();
};


var doCreate = function() {
    var dataFinePrevista = new Date();
    var el = document.getElementById("dataText");
    ordini = JSON.parse(el.value);
    console.dir(ordini);

    $("#form").hide();

    for (var i = 0; i<ordini.length; i++) {
        orp = ordini[i];
        fasi = orp.FASI;
        var orpTipo = (orp.flag_matricola == "1") ? "M" : (orp.flag_lotto == "1") ? "L" : "A";
        if (orp.data_fine_prevista!="") {
            var arrData = orp.data_fine_prevista.split(".");
            dataFinePrevista.setFullYear(arrData[2],arrData[1]-1,arrData[0]);
        } else {
            dataFinePrevista = null;
        }
        nomeScheda = orp.doc_id + " | " + orp.cod_art + " | " + orp.quant_da_prod + " | " + orpTipo;
        var newCard = 
            {
            name: nomeScheda, 
            desc: "Data documento: "+orp.data_doc +"\n"+ ((orpTipo=="L")?"Lotto: "+orp.cod_lot+"\n":"") + JSON.stringify(orp),
            pos: "top", 
            due: dataFinePrevista.toISOString(),
            idList: LISTAid
            };
        Trello.post('/cards/', newCard, tcard_success, t_error);
    }  
};

function CreateChecklist(card) {
    var newList = 
        {
        idCard:card.id,
        name:"Cicli di lavorazione",
        pos:"top"
        }
    Trello.post('/checklists/', newList, CreateCheckListItems, t_error);
}

function CreateCheckListItems(checkList) {
    console.log("Checklist created (id="+checkList.id+"):");
    console.dir(checkList);
    //var newItem = "TESTNAME"; // WRONG version
    var newItem = {name:"TESTNAME"}; // CORRECTED version. Thanks Casey
    Trello.post('/checklists/'+checkList.id+'/checkItems/', newItem, null, t_error);    
}



Trello.authorize({
    interactive:false,
    success: onAuthorize
});

$("#connectLink")
.click(function(){
    Trello.authorize({
        type: "popup",
        scope: { read: true, write: true },
        success: onAuthorize
    })
});

$("#disconnect").click(logout);

$("#btn").click(doCreate);


});//]]> 

</script>

<div id="loggedout">
    <a id="connectLink" href="#">Connect To Trello</a>
</div>

<div id="loggedin">
    <div id="header">
        Logged in to as <span id="fullName"></span> 
        <a id="disconnect" href="#">Log Out</a>
    </div>
    <div id="form">
      <h3>ORDINI</h3>
      <textarea id="dataText" name="testo" rows="12" cols="80">[
  {
    "doc_id": "2011-ORP-0000126",
    "data_doc": "24.03.2011",
    "cod_art": "4010103000",
    "quant_da_prod": "1500",
    "data_fine_prevista": "20.04.2011",
    "flag_matricola": "0",
    "flag_lotto": "0",
    "cod_lot": "",
    "FASI": [
      {
        "riga": "1",
        "flag_ciclo_est": "1",
        "cod_ciclo": "029",
        "des_ciclo": "COSTRUZIONE",
        "cod_cf": "000261",
        "rag_soc_cf": "SOME COMPANY SRL"
      }
    ]
  }
]</textarea><br/><br/>
      <a id="btn" href="#">ELABORA</a>
    </div>
    <div id="output"></div>
</div>    
</body>
</html>

1 个答案:

答案 0 :(得分:0)

看起来您没有使用格式正确的JSON填充项目。

尝试将newItem更改为以下内容。

var newItem = { "name": "TESTNAME" };

有关其他输入值的信息,请参阅API文档:https://developers.trello.com/advanced-reference/checklist#post-1-checklists-idchecklist-checkitems

希望这有帮助!