Search code examples
javascriptjquerytrello

Get Trello card comments using Javascript / jQuery


I'm trying to get the card comments and put then in some html tag.

I do get then (I guess) doing this:

Trello.get("cards/6a7c530bce987f166f2272ef/actions", function(err, card) {

  console.log(card); // I see the comments in console

  $('#comments').html(card); // shows "success" in the page

})

In console, I see the comments:

Console in Chrome

But I don't know how to use "card" object. Using JSON.stringify(card) and log to conole, returns "success".

I tried "card[0]", "card['text']", "card.text", "card.data.text" but nothing seens to work.

So, I need help to get the card comments from "card" object or another way.

UPDATE: The error was the position of function arguments. Wrong: "(err, card)". Correct: "(card, err)". To access a specific comments, the following works fine:

var last_comment = card[0]['data']['text']; // worked!!!

Solution

  • The screenshot you sent shows that the request returns an array of comment cards. If you wish to display all the comments, you could use the following code:

    Trello.get("cards/6a7c530bce987f166f2272ef/actions", function(err, comments) {
      var html = "";
      comments.forEach(function(comment) {
        html += comment.data.text+"<br>";
      }
      $('#comments').html(html);
    });
    

    If you just want the first comment in the array, you can do the following:

    Trello.get("cards/6a7c530bce987f166f2272ef/actions", function(err, comments) {
      $('#comments').html(comments[0].data.text);
    });
    

    I would recommend having a read on Working with objects and Arrays.