Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
•
IT Security NachrichtenUniversal-Achterbahn: Schreie der Fahrgäste nerven Nachbarschaft(01.10.2026 um 14:29 Uhr)
•
Sichere ProgrammierungGitHub Release: rust-lang/rust v1.99.0 (01.10.2026)(01.10.2026 um 14:41 Uhr)
•••
IT NachrichtenAtos invests in AI education for West Midlands youth(01.10.2026 um 10:43 Uhr)
••••••
IT Security NachrichtenUniversal-Achterbahn: Schreie der Fahrgäste nerven Nachbarschaft(01.10.2026 um 14:29 Uhr)
•
Sichere ProgrammierungGitHub Release: rust-lang/rust v1.99.0 (01.10.2026)(01.10.2026 um 14:41 Uhr)
•••
IT NachrichtenAtos invests in AI education for West Midlands youth(01.10.2026 um 10:43 Uhr)
•••••
Intelligence View
⚡ tsecurity.de Intelligence

contentful-managementでエントリーの一覧取得と新規作成をasyncでやってみる

Using async with contentful-management to List and Create Entries I recently experimented with the JavaScript SDK for contentful-management and wanted to jot…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!




Using async with contentful-management to List and Create Entries



I recently experimented with the JavaScript SDK for contentful-management and wanted to jot down my findings before I forget.






Preparation



Create a Contentful account and space. This article helped me understand the process clearly.



Install the necessary packages for your Node.js project:




npm install dotenv
npm install contentful-management






Create a .env file in the same directory as your package.json and add the following:




personal_access_token=your_personal_access_token
space_id=your_space_id
environment_id=your_environment_id_or_master






You need to create a Personal Access Token specifically for the Contentful Management API, separate from the Delivery and Preview API keys. For details, check the official page.






Retrieving a List of Entries from Contentful



In Contentful, each piece of data is called an entry, similar to a record in a relational database.



Here's the source code to obtain a list of entries:




require('dotenv').config()
const env = process.env

process.on('unhandledRejection', console.dir);

const contentful = require('contentful-management')
const client = contentful.createClient({
accessToken: env.personal_access_token
});

(async () => {
const space = await client.getSpace(env.space_id);
const environment = await space.getEnvironment(env.environment_id);
const entries = await environment.getEntries();
entries.items.forEach(item => console.log(item));
})();









Registering an Entry in Contentful



A content model in Contentful is akin to a table in a database. We will create a new entry in this model. First, create it as a draft, and then publish it.



Here's the source code:



The fields section varies depending on your content model, so adjust as necessary. If you've changed Contentful's Settings > Locales to ja, replace en-US with ja.




require('dotenv').config()
const env = process.env

process.on('unhandledRejection', console.dir);

const contentful = require('contentful-management')
const client = contentful.createClient({
accessToken: env.personal_access_token
});

(async () => {
const space = await client.getSpace(env.space_id);
const environment = await space.getEnvironment(env.environment_id);

const draftEntry = await environment.createEntry('hoge_content_type', {
fields: {
id: {
'en-US': 'hoge_id'
},
name: {
'en-US': 'hoge_name'
}
}
});

try {
const publishedEntry = await draftEntry.publish();
console.log(`Published entry ID: ${publishedEntry.sys.id}`);
} catch (err) {
console.error(err);
}
})();






Remember to change 'hoge_content_type' to your content model's ID as needed.






In Conclusion



I referred to the following pages when creating this article. Many thanks for the clear information.



Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten contentful-managementでエントリーの一覧取得と新規作成をasyncでやってみる

Thematisch verwandte Begriffe: contentfulmanagementでエントリーの一覧取得と新規作成をasyncでやってみる · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag