如何获取Trello看板中所有已配置的customFieldItems(含无值项)
The issue you’re facing is that the standard Trello API endpoint for cards only returns customFieldItems when a value has been explicitly set on the card. To retrieve all custom fields configured for the board—even those with no values on specific cards—you’ll need to combine two API calls and merge the results. Here’s how to do it:
Step 1: Fetch All Custom Fields Defined on the Board
First, get a complete list of every custom field configured for your board. This includes fields that might not have values set on any cards yet. Use this endpoint:
GET https://api.trello.com/1/boards/<board-id>/customFields?key=<your-api-key>&token=<your-api-token>
This returns an array of objects, each representing a custom field with details like id, name, type, and (for dropdowns) options.
Step 2: Fetch All Cards with Existing Custom Field Values
Use your original endpoint to pull all cards and their current customFieldItems:
GET https://api.trello.com/1/boards/<board-id>/cards/?fields=all&customFieldItems=true&key=<your-api-key>&token=<your-api-token>
As you noted, each card’s customFieldItems array only includes entries for fields that have a value set.
Step 3: Merge Board Custom Fields with Card Data
For each card, combine the board’s full list of custom fields with the card’s existing customFieldItems. For fields the card doesn’t have a value for, add an entry with an appropriate empty value based on the field type.
Here’s a practical JavaScript example of this merging logic:
async function getCardsWithAllCustomFields(boardId, apiKey, apiToken) { // Fetch all custom fields from the board const customFieldsRes = await fetch( `https://api.trello.com/1/boards/${boardId}/customFields?key=${apiKey}&token=${apiToken}` ); const allBoardFields = await customFieldsRes.json(); // Fetch cards with their existing custom field values const cardsRes = await fetch( `https://api.trello.com/1/boards/${boardId}/cards/?fields=all&customFieldItems=true&key=${apiKey}&token=${apiToken}` ); const cards = await cardsRes.json(); // Merge fields for each card to include empty ones return cards.map(card => { // Map existing fields by ID for quick lookup const existingFieldsMap = new Map( card.customFieldItems.map(item => [item.idCustomField, item]) ); // Build full customFieldItems array const fullCustomFields = allBoardFields.map(field => { if (existingFieldsMap.has(field.id)) { return existingFieldsMap.get(field.id); } // Set empty value based on field type let emptyValue; switch (field.type) { case 'text': emptyValue = { text: '' }; break; case 'number': emptyValue = { number: null }; break; case 'date': emptyValue = { date: null }; break; case 'checkbox': emptyValue = { checked: false }; break; case 'list': emptyValue = { idValue: null }; break; default: emptyValue = {}; } return { idCustomField: field.id, idValue: null, value: emptyValue }; }); return { ...card, customFieldItems: fullCustomFields }; }); }
Key Notes
- Field Type Handling: Empty values vary by field type (e.g., checkboxes default to
false, text fields to empty strings). Adjust this logic if your board uses less common field types. - Performance: For large boards, add pagination (using
limitandpageparameters) to the cards endpoint to avoid rate limits or timeouts. - Permissions: Make sure your API key and token have read access to both board custom fields and card data.
内容的提问来源于stack exchange,提问作者CodeBrauer

