2016-05-16 65 views
1

我成功創建了卡和清單,但我無法填充新創建的清單和所需的項目。 Trello的確對400的「無效值的名稱」作出了響應 我越來越瘋狂,試圖找出以下代碼中的錯誤。 有沒有人可以幫忙?Trello API:如何填充清單?

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> 

回答