首頁 > web前端 > js教程 > 主體

某物

Mary-Kate Olsen
發布: 2024-10-28 04:26:31
原創
604 人瀏覽過

某物

async function getMediumPosts() {
    try {
        const response = await fetch('https://api.medium.com/v1/users/@sukhrobtech/posts', {
            headers: {
                Authorization: `Bearer 29a6e098e7413bb577279281bb650edd904329dfc1561bfc687600f4ca656609b`,
            },
        });

        if (!response.ok) {
            throw new Error(`Error: ${response.status} ${response.statusText}`);
        }

        const data = await response.json();
        return data.data; // Assuming that posts are in the `data` field of the response
    } catch (error) {
        console.error("Failed to fetch Medium posts:", error);
        return []; // Return an empty array if there's an error
    }
}

const Page = async () => {
    const posts = await getMediumPosts();

    return (
        <div>
            <h2>My Medium Articles</h2>
            <ul>
                {posts.length > 0 ? (
                    posts.map((post) => (
                        <li key="{post.id}">
                            <a href="%7Bpost.url%7D" target="_blank" rel="noopener noreferrer">
                                {post.title}
                            </a>
                        </li>
                    ))
                ) : (
                    <li>No articles found.</li>
                )}
            </ul>
        </div>
    );
};

export default Page;

登入後複製

以上是某物的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!