jimmyLi
jimmyLi

Reputation: 93

Trello API - connect cards with boards to get board name based on the board id

So I got all the cards from all the lists, inside all my boards:

var url3 = "https://api.trello.com/1/lists/"+listId+"/cards?key="+key+"&token="+token;

Now I need to join somehow with the boards list and get the board name of each card, based on the board ids, and I don't know how...

This is my entire function, trying to get all the cards with a due date, but I also want to get the name of the board each card is in...

function getDues(listId){
    var xmlhttp3 = new XMLHttpRequest();
    var url3 = "https://api.trello.com/1/lists/"+listId+"/cards?key="+key+"&token="+token;
    xmlhttp3.onreadystatechange = function() {
        if(xmlhttp3.readyState == 4 && xmlhttp3.status == 200) {
            var arrCards = JSON.parse(xmlhttp3.responseText);
            var dueName = "";
            var dueDate = "";
            var board = "";
            for(c = 0; c < arrCards.length; c++){
                if(arrCards[c].due !== null){
                    var dueName = arrCards[c].name;
                    var dueDate = arrCards[c].due;
                    var boardNo = arrCards[c].idBoard;
                    var date = new Date(dueDate);
                    var deadline = "";
                    var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
                    var now = new Date();
                    var result = "https://api.trello.com/1/organizations/"+orgId+"/boards?key="+key+"&token="+token;
                    for (card in arrCards) {
                      if (!arrCards.hasOwnProperty(card)) { continue; }
                      var name = result.name;
                      card.boardName = name
                      console.log(card.boardName);
                    }
                }
            }
        }
    }
    xmlhttp3.open("GET", url3, true);
    xmlhttp3.send();
}

All I get in for board name is "undefined".

Update

Changed the way I got the cards, and like this it works:

function getDues(boardId) {
  var xmlhttp3 = new XMLHttpRequest();
  var url3 = "https://api.trello.com/1/boards/"+boardId+"/cards?key="+key+"&token="+token;
  xmlhttp3.onreadystatechange = function() {
    if (xmlhttp3.readyState === 4 && xmlhttp3.status === 200) {
      var arrCards = JSON.parse(xmlhttp3.responseText);
      var dueName = "";
      var dueDate = "";
      var board = "";
      for (var c = 0; c < arrCards.length; c++) {
        if (arrCards[c].due !== null) {
          var dueName = arrCards[c].name; // due card name
          var dueDate = arrCards[c].due; // due date
          var boardNo = arrCards[c].idBoard; // board id
          var date = new Date(dueDate);
          var deadline = "";
          var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
          var now = new Date();

          var xmlhttpBoardName = new XMLHttpRequest();
          var boardNameUrl =  "https://api.trello.com/1/boards/" + boardNo + "/?key=" + key + "&token=" + token;

          xmlhttpBoardName.onreadystatechange = function() {
            if (xmlhttpBoardName.readyState === 4 && xmlhttpBoardName.status === 200) {
                var board = JSON.parse(xmlhttpBoardName.responseText);
                var boardName = board.name; // board name
            }
          }

          xmlhttpBoardName.open("GET", boardNameUrl, true);
          xmlhttpBoardName.send();
        }
      }
    }
  }

  xmlhttp3.open("GET", url3, true);
  xmlhttp3.send();
}

Upvotes: 0

Views: 412

Answers (1)

rpadovani
rpadovani

Reputation: 7360

Basing on your code, something like this should work:

function getDues(listId) {
  var xmlhttp3 = new XMLHttpRequest();
  var url3 = "https://api.trello.com/1/lists/"+listId+"/cards?key="+key+"&token="+token;
  xmlhttp3.onreadystatechange = function() {
    if (xmlhttp3.readyState === 4 && xmlhttp3.status === 200) {
      var arrCards = JSON.parse(xmlhttp3.responseText);
      var dueName = "";
      var dueDate = "";
      var board = "";
      for (var c = 0; c < arrCards.length; c++) {
        if (arrCards[c].due !== null) {
          var dueName = arrCards[c].name;
          var dueDate = arrCards[c].due;
          var boardNo = arrCards[c].idBoard;
          var date = new Date(dueDate);
          var deadline = "";
          var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
          var now = new Date();

          var xmlhttpBoardName = new XMLHttpRequest();
          var boardNameUrl =  "https://api.trello.com/1/boards/" + boardNo + "/?key=" + key + "&token=" + token;

          xmlhttpBoardName.onreadystatechange = function() {
            if (xmlhttpBoardName.readyState === 4 && xmlhttpBoardName.status === 200) {
              var board = JSON.parse(xmlhttpBoardName.responseText);
              var boardName = board.name; // HERE YOU HAVE YOUR BOARD NAME
              console.log(boardName);
            }
          }

          xmlhttpBoardName.open("GET", boardNameUrl, true);
          xmlhttpBoardName.send();
        }
      }
    }
  }

  xmlhttp3.open("GET", url3, true);
  xmlhttp3.send();
}

Anyway, I suggest you to use the official library, you do not have to take care of all the http requests.

Upvotes: 1

Related Questions