I am trying to get the top card from a list using Javascript. I saw another post on this but the code was outdated / no longer working.
I have a Trello list, called "Flights" and I would appreciate if someone could help me with this. Simply, I want to get the top card in a Trello list, along with the custom fields of the card.
So far, I have tried the following code:
// Getting all lists from a certain board.
fetch(`https://api.trello.com/1/boards/0Ph2WHXQ/lists?key=5fe33d73bded56ada14decf701da0203&token=e353cb19fabc8dfbea36e1b824a92ff0b358860b1de2d11c745f6e8321edfc09`, {
method: 'GET',
}).then(response => {
console.log(response)
let lists = JSON.parse(response.body);
// Searching for a list with a certain name and store it's id.
let certainListId = lists.find(list => list.name === "Flights").id;
// Use the stored id in certainListId to get all cards of the list.
fetch(`https://api.trello.com/1/lists/${certainListId}/cards`, {
method: 'GET'
}).then(response => {
let certainListCards = JSON.parse(response.body);
// Cards in the array is in top to bottom order. So top one is the very first.
let firstCard = certainListCards[0];
console.log(firstCard)
// Send with the bot the name of the first (top) card thanks to firstCard.name property.
}).catch(err => console.error(err));
}).catch(err => console.error(err));
However, I get the following error message:
The error is on the line:
let lists = JSON.parse(response.body);
I have tried the code without the JSON.parse, but I have an error "Typeerror: lists.find is not a function"
I assume I need the parse code, but I have zero idea why it's not working. I have attached an image of just printing the response.body to the console.
Thank you for reading this, and I would highly appreciate some help. I am using node.js (latest version), using the cross-fetch module for the 'fetch' command. I am just requiring it as
const fetch = require("cross-fetch");
I highly doubt that would cause any issues, but feel free to enlighten me. Thanks again!