Error: Objects are not valid as a React child (found: [object Promise]). in MDX remote

I’m trying to use next.js to get all of the blogs on a mongodb backend, and converting all of the blog strings stored in blog.mdx into an array of objects wrapped in

  • wrappers. However, the code below gives me the error that:
    Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
    I’m not really sure what’s the fix to this. I’ve followed this stackoverflow question for help but it hasn’t fixed my issue

    import Head from "next/head";
    import clientPromise from "../lib/mongodb";
    import type { InferGetServerSidePropsType, GetServerSideProps } from "next";
    import {useEffect, useState} from "react";
    import {MDXRemote} from "next-mdx-remote/rsc";
    import { serialize } from 'next-mdx-remote/serialize'
    import React from "react";
    
    type ConnectionStatus = {
        isConnected: boolean;
    };
    
    export const getServerSideProps: GetServerSideProps<
        ConnectionStatus
    > = async () => {
        try {
            await clientPromise;
            // `await clientPromise` will use the default database passed in the MONGODB_URI
            // However you can use another database (e.g. myDatabase) by replacing the `await clientPromise` with the following code:
            //
            // `const client = await clientPromise`
            // `const db = client.db("myDatabase")`
            //
            // Then you can execute queries against your database like so:
            // db.find({}) or any of the MongoDB Node Driver commands
    
            return {
                props: { isConnected: true },
            };
        } catch (e) {
            console.error(e);
            return {
                props: { isConnected: false },
            };
        }
    };
    
    const Home = ({ isConnected }: InferGetServerSidePropsType<typeof getServerSideProps>) => {
        const [blogs, setBlogs] = useState([]);
        useEffect(() => {
            async function fetchAllBlogs() {
                const response = await fetch("http://localhost:3000/api/getAllBlogs");
                const blogs = await response.json();
                const blogMDX = await Promise.all(blogs.map(async (blog) => (
                    <li key={blog._id}>
                        <MDXRemote {...(await serialize(blog.mdx))} />
                    </li>
                )))
                console.log("blogs ", blogs);
                console.log("blogsMDX ", blogMDX);
                setBlogs(blogMDX);
            }
            fetchAllBlogs();
        }, []);
    
    
        return (
            <div className="container">
                <Head>
                    <title>Create Next App</title>
                    <link rel="icon" href="/favicon.ico"/>
                </Head>
    
                <main>
                    <div>
                        {isConnected ? (
                            <h2 className="subtitle">You are connected to MongoDB</h2>
                        ) : (
                            <h2 className="subtitle">
                                You are NOT connected to MongoDB. Check the <code>README.md</code>{" "}
                                for instructions.
                            </h2>
                        )}
                    </div>
                    <div>
                        {
                            blogs == null ? (
                                <div></div>
                            ) :(
                                blogs.length > 0 ? (
                                    <ul>
                                        {blogs}
                                    </ul>
                                ) : (
                                    <p>Loading...</p>
                                )
                            )
                        }
                    </div>
                    <div>
    
                    </div>
                </main>
    
    
            </div>
        );
    }
    
    export default Home;
    

    I’ve tried converting my

    
    <ul>
        {blogs}
    </ul>
    

    to

    blogs.map( (blog:any, index) => (
                                        <React.Fragment key={index}>
                                            {blog}
                                        </React.Fragment>
                                    ))
    ```,
    but while this gets rid of the error, the MDXRemote no longer shows up.
    
    The console.logs print out this:
    
    

    blogs
    Array [ {…} ]

    0: Object { _id: “66358b465fb668714ea217b3”, mdx: “—ntitle: Example Postnpublished: 2021-02-13ndescription: This is some descriptionn—nnnnHere’s a neat demon:” }

    length: 1

    : Array []
    index.tsx:50:20
    blogsMDX
    Array [ {…} ]

    0: Object { “$$typeof”: Symbol(“react.element”), type: “li”, key: “66358b465fb668714ea217b3”, … }

    length: 1

    : Array []